Group cell array and vector for "for loop and if statement"
Show older comments
I want to choose submatrices from the table below.

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:

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.
Gko K
on 1 Mar 2019
Guillaume
on 1 Mar 2019
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.
Gko K
on 1 Mar 2019
Guillaume
on 1 Mar 2019
So, in the above example, what should the output be?
Gko K
on 1 Mar 2019
Guillaume
on 1 Mar 2019
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.
Gko K
on 1 Mar 2019
Answers (2)
Andrei Bobrov
on 1 Mar 2019
0 votes
mat.xlsx - file with your data
T = readtable('mat.xlsx');
[~,jj] = sortrows(T(:,2:4))
out = T(jj,:);
Guillaume
on 1 Mar 2019
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
Gko K
on 1 Mar 2019
Guillaume
on 1 Mar 2019
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.
Gko K
on 1 Mar 2019
Guillaume
on 4 Mar 2019
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.
Gko K
on 13 Mar 2019
Guillaume
on 13 Mar 2019
Sorry, but no. I don't do consultancy.
Gko K
on 15 Mar 2019
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

