Asked by Yannick Tabot Njami
on 13 Mar 2019

Dear All i need help with sorting a table and also new to matlab your time is appreciate

Please consider the attachment

Here is my algorithm :

ycoordinate= [20 50 80 110 140 170 200 230]

- find all index example when y= 20
- get the x coordinate values corresponding to to the indices y=20
- get the corresponding mean_velocity,std_velocity, mean_pd ,std_pd vlalues
- get them as a pair. meaning that for case when y=20 ,
- x = [20 110 140 170 200 20 230 50 80] and mean_velocity=[1.3696 57.5733 57.7197 57.7133 58.0707 58.0194 58.1603 57.9150 57.7572]
- then sorting them in ascending or descending order as
- x=[20 20 50 80 110 140 170 200 230] mean_velocity=[1.3696 58.0194 57.9150 57.7572 57.5733 57.7197 57.7133 58.0707 58.1603 ]
- and then plot mean_velocity against x. i want to do this for the the std_velocity, mean_pd ,std_pd as well.
- i thought about a for loop but dont know how to go about it . please any help will be greatly appreciated
- many thanks in advance

Answer by Guillaume
on 13 Mar 2019

If I understood correctly what you want, you basically want a plot for each unique y value which are the other variables against x. The easiest way to achieve this is:

- create a function to plot the variables against x, ignoring y for now. That function therefore takes as input x, and the other variables. E.g (tailor as required):

function myplot(x, varargin) %saved in its own m file

%inputs:

% x: a possibly unsorted array of x coordinates. Will be sorted before plotting

% 4 more inputs (for now): mean velocity, std velocity, mean pd, std pd.

%all inputs are column vector as they come from a table

assert(numel(varargin) == 4, 'Only 4 variables expected');

[x, order] = sort(x);

plotvars = [varargin{:}]; %concatenate the other 4 vectors into a 4 column matrix

plotvars = plotvars(order, :); %and sort in the same order as x

plot(x, plotvars);

legend({'mean velocity', 'std velocity', 'mean pd', 'std pd'});

end

- use rowfun to call the above function for each group of unique y:

rowfun(@myplot, thetable, 'GroupingVariables', 'ycoordinates', 'InputVariables', {'xcoordinates', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'}); %group by y. For each group, pass x and the other columns to the function

Done!

Guillaume
on 15 Mar 2019

It probably would make sense to create a new figure for each y, and use the y to name the graph. So I'd modify the myplot function to:

function myplot(y, x, varargin) %saved in its own m file

%inputs:

% y: the y coordinate of the plot. Due to the way rowfun works this will be a column vector of identical y

% x: a possibly unsorted array of x coordinates. Will be sorted before plotting

% 4 more inputs (for now): mean velocity, std velocity, mean pd, std pd.

%all inputs are column vector as they come from a table

assert(numel(varargin) == 4, 'Only 4 variables expected');

figure;

[x, order] = sort(x);

plotvars = [varargin{:}]; %concatenate the other 4 vectors into a 4 column matrix

plotvars = plotvars(order, :); %and sort in the same order as x

plot(x, plotvars);

legend({'mean velocity', 'std velocity', 'mean pd', 'std pd'});

title(sprintf('At y = %g', y(1)));

end

and the rowfun:

rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'ycoordinate', 'xcoordinate', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'}, 'NumOutputs', 0);

"but the plot produce was very strange and not what i expected"

Well without any detail of what you expected and what you got, it's difficult to give any advice.

Yannick Tabot Njami
on 15 Mar 2019

Here is what i get :

Yannick Tabot Njami
on 15 Mar 2019

here is what i expect :

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## dpb (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/449851-sorting-a-64-x6-table#comment_680612

## Guillaume (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/449851-sorting-a-64-x6-table#comment_680616

## Guillaume (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/449851-sorting-a-64-x6-table#comment_680630

Sign in to comment.