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

3 comentarios

dpb
dpb el 13 de Mzo. de 2019
Much easier to debug and show where make either syntax or logic errors if you will actually insert the code itself, not just the description. It's mandatory to have the problem description of what you're trying to do, of course, but "show your work!", too...
As to the Q? SORT() will do what you want...you can either sort the array with _'rows'_ option or you can sort the one vector and return the sort order index to rearrange the other variables accordingly. Syntax and examples at
doc sort
Guillaume
Guillaume el 13 de Mzo. de 2019
For table, sortrows is probably more appropriate than sort. You can pass the variable names directly to sortrows to prioritise the sort columns:
sortrows(sometable, {'ycoordinates', 'xcoordinates'}) %to sort first by y, then x.
Guillaume
Guillaume el 13 de Mzo. de 2019
Yannick comment posted as an answer moved here (it's just code with no explanation):
Average_Table = table(xcoordinate,ycoordinate,mean_velocity,std_velocity, mean_pd ,std_pd);
for jj = 20 :30 :230
yindex=find(ycoordinate == jj);
%Deleting the first index to eliminat the offset value at y=20
if jj ==20
yindex(yindex==1)=[];
end
% getting just a table dor x and mean_velocity
newX=(xcoordinate(yindex));
newVY =mean_velocity(yindex);
%generated table as a pair
tableXY =[newX newVY]
% soting out the table
preallocating memory for sorted table
sortedtable =zeros(length(tableXY),2)
sortedtable(:,1)=sort(tableXY(:,1))
sortedX = sortedtable (:,1)
%Going through each table to look for corresponding indeces and match .
for N =1:length(tableXY)
sortedtable(N,2:end) =tableXY(find(ismember(tableXY(:,1),sortedtable(N))),2)
% sorted Y coordinates
sortedY = sortedtable(:,2);
end
hold on
plot(sortedX,sortedY,'color',rand(1 ,3),'marker','*');
xlabel('x position');
ylabel('mean velocity');
legend(['mean velocity against position =' num2str(jj)]);
drawnow;

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 13 de Mzo. de 2019

0 votos

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!

11 comentarios

Yannick Tabot Njami
Yannick Tabot Njami el 13 de Mzo. de 2019
Editada: Yannick Tabot Njami el 13 de Mzo. de 2019
@ Guillaume many thanks for the help .
As a leaner am still struggling to understand the the code and then appl it to my case :)
i will get back to you about this
my appreciation
Guillaume
Guillaume el 13 de Mzo. de 2019
Editada: Guillaume el 13 de Mzo. de 2019
All you need to do for now, is save the function in its own m file, myplot.m.
Then replace the whole of your for jj = 20 :30 :230 loop by the one-line rowfun, so the code you wrote above becomes:
Average_Table = table(xcoordinate,ycoordinate,mean_velocity,std_velocity, mean_pd ,std_pd);
rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'xcoordinate', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'});
This will produce your plots (unless I've made a mistake of course). You can then tailor the myplot function to adjust the plots so they look the way you want.
Yannick Tabot Njami
Yannick Tabot Njami el 13 de Mzo. de 2019
Editada: Yannick Tabot Njami el 13 de Mzo. de 2019
i did what you just said but it says function or variable Average_Table is undefined.
dont kno why .
All together i should have 8*4 = 32 plots since each parameter has 8 values i.e for each y where y =[20 50 80 110 140 170 200 230]
Guillaume
Guillaume el 13 de Mzo. de 2019
Please give the full text of the error message. Everything that is in red.
The two lines of code above certainly can't throw that error. The first line (which you wrote) creates Average_Table. So it's definitively defined in the second line.
Guillaume
Guillaume el 13 de Mzo. de 2019
Yannick comment mistakenly posted as an answer moved here:
Here the function in its own m file :
function myplot(x, varargin)
assert(numel(varargin)==4)
[x,order] =sorte(x);
plotvars = [varargin{:}];
plotvars =plotvars(order,:);
plot(x,plotvars);
legend({'mean velocity', 'std velocity', 'mean pd', 'std pd'})
end
% THe replaced line with whole of my for loop for jj=20:30:230
rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'xcoordinate', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'});
here the error:
Error using tabular/rowfun>dfltErrHandler (line 497)
Applying the function 'myplot' to the 1st group of rows in A generated the following error:
Too many output arguments.
.
Error in tabular/rowfun>@(s,varargin)dfltErrHandler(grouped,funName,s,varargin{:}) (line 268)
errHandler = @(s,varargin) dfltErrHandler(grouped,funName,s,varargin{:});
Error in tabular/rowfun (line 288)
[b_data{igrp,:}] = errHandler(struct('identifier',ME.identifier, 'message',ME.message, 'index',igrp),inArgs{:});
Error in nuzzle_20190301 (line 88)
rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'xcoordinate', 'mean_velocity', 'std_velocity',
'mean_pd', 'std_pd'});
>>
Guillaume
Guillaume el 13 de Mzo. de 2019
Editada: Guillaume el 13 de Mzo. de 2019
Can you save your Average_Table in a mat file and attach that to your question. It will be a lot more useful than a screenshot. With actual data, we can actually test our answers.
edit:
Oh, of course, I forgot to tell rowfun that myplot doesn't output anything. Add 'NumOutputs', 0 to the rowfun call:
rowfun(@myplot, Average_Table, 'GroupingVariables', 'ycoordinate', 'InputVariables', {'xcoordinate', 'mean_velocity', 'std_velocity', 'mean_pd', 'std_pd'}, 'NumOutputs', 0);
Guillaume
Guillaume el 15 de Mzo. de 2019
Editada: Guillaume el 15 de Mzo. de 2019
Yannick's comment incorrectly posted as an answer moved here:
Hi Guillaume
attached is the table as a txt file .
can you convert to .mat extention and visualize it better ?
thanks
Guillaume
Guillaume el 15 de Mzo. de 2019
Yannick's comment incorrectly posted as an answer moved here:
Hello Guillaume
thanks the code did ran after the adjustment. but the plot produce was very strange and not what i expected. However many thanks
Guillaume
Guillaume el 15 de Mzo. de 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
Yannick Tabot Njami el 15 de Mzo. de 2019
Here is what i get :untitled.png
Yannick Tabot Njami
Yannick Tabot Njami el 15 de Mzo. de 2019
here is what i expect :dataplot.png

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 13 de Mzo. de 2019

Comentada:

el 15 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by