Group cell array and vector for "for loop and if statement"

I want to choose submatrices from the table below.
mat.jpg
For example, i want to choose first E01 values from column "Name" and after C2 MAX values form column "Case", and after Down values from column "Loc". I want to make that for every combination of this matrix. One of an example matrix should be as below:
mat2.jpg
How can i do this? After i can do this selection and get every combinations i will calculate some values from every seperate statement.
Who can help me ? :)

8 Comments

It looks like your "matrix" is an excel file (or similar). Can you attach an example file to your question?
" i will calculate some values"
More details about that part would allow us to give you a better answer. Depending on what you're planning to calculate, the whole thing can be done in just one line.
Sorry, i have sent message as answer, not as comment. You can see my message as answer 3 below.
Yes, please use comments instead of answers. If people see that your question already has answers they may not event look at it.
Again, what calculations do you want to do? As I said, depending on what they are, the calculation could be done in just one line for all the groups.
I will choose matrix as below:
mat4.jpg
After filtering as above for all conditions i will interpolate S2 values between max(S2) and 0.5*max(S2) values for all submatrices. Every submatirx calculation should be separate.
Here is my excel file.
So, in the above example, what should the output be?
I want to get something like that:
mat5.jpg
For all condition..Seperate plots and vectors.
Ok, now it's clearer. Where does the Y come from? Is it just 1:10 in the order the values come in? Or the reverse of the numbers in the S column?
For E01-C2 MIN-Down, the values of S2 are:
-41.75
-40.97
-47.91
-53.96
-61.02
-71.36
-86.36
-104.51
-118.55
2.41
The maximum of that is 2.41. According to your rule, we would interpolate between 1.21 and 2.41. I presume that's not what you want.
Similarly, for E01-C2 MIN-Up, the values are
-47.4
-40.06
-47.82
-53.98
-61.06
-71.16
-85.82
-104.14
-117.93
-26.09
With maximum -26.09. So an interpolation between -13.05 and -26.09. Presumably not what you want.
Y come from S column as S10:S1.
I mean absolute max. values SO not 2.41, it should be -118.55.
Can you group matrices for all conditon? I want to repeat interpolation operation for all condition group and plot it.

Sign in to comment.

Answers (2)

mat.xlsx - file with your data
T = readtable('mat.xlsx');
[~,jj] = sortrows(T(:,2:4))
out = T(jj,:);
I do not really understand your interpolation at all. I do not understand how your interpolated values go from 77.28 when half of max is 69.08 nor why 138 is repeated.
Anyway, here is some code to do part of what you want. You'll need to modify the interpolation code to do exactly what you want.
First, create a function to do the plotting for a group:
function groupplot(s2, name, casename, loc)
%s2: the value of column S2 for the group
%name: the name of the group. Will come as a cell array with all identical values
%casename: the case of the group. Will come as a cell array with all identical values
%loc: the loc of the group. Will come as a cell array with all identical values
[absmax, row] = max(abs(s2)); %find the absolute maximum and its location
sgn = sign(s2(row)); %sign of the maximum
s2interpolated = interp1(s2, s2, sgn * linspace(0.5*absmax, absmax, 10)); %not what you want but I don't understand what you want
figure;
plot(s2, 10:-1:1, s2interpolated, 10:-1:1);
legend('Original data', 'interpolated data');
title(sprintf('%s - %s - %s', name{1}, casename{1}, loc{1}));
end
and save that.
To apply this to your data:
data = readtable('data.xlsx');
rowfun(@groupplot, data, 'GroupingVariables', {'Name', 'Case', 'Loc'}, 'InputVariables', {'S2', 'Name', 'Case', 'Loc'}, 'NumOutputs', 0);
rowfun takes care of splitting the data into groups and calling the function for each group.

7 Comments

Interpolation is not important for the case. You can think it any other operation as adding values.Something like this:
mat6.jpg
I will try your code to group the data when i get home. Thank you for your help. It is the hard part for me to group data.
In my answer, the grouping of the data is the easiest part, matlab does all the hard work for you, it's just a case of passing 'GroupingVariables' and the name of the variables used for grouping (in the correct order) to rowfun. The hardest bit is actually writing the code for each group.
There are other ways of performing the grouping which may be more appropriate depending on what the ultimate result has to be. One of these is splitapply. Another way is to actually split the data into separate arrays and use loops but that should be avoided as much as possible. It's simpler to let matlab do that job for you.
Thanks for your help, i try this code but something went wrong. I will try to solve that by myself. If i cant solve it, could you help me for the later operations? I am not good at programming, i am beginner about matlab :)
i try this code but something went wrong
Then if you want some help, give us the details. If you get an error, give the full text of the error message.
I've tested my answer on the spreadsheet you provided and it worked without any issue.
hello friend, can you send your email, i want to tell the whole operation which i want to do in matlab:)
Thank you so much for your help:)
Sorry, but no. I don't do consultancy.
Ok friend, thank you for all your help :)

Sign in to comment.

Products

Release

R2018a

Asked:

on 1 Mar 2019

Commented:

on 15 Mar 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!