Sort and extract elemets from a structure.
1 view (last 30 days)
Show older comments
Hello all,
I have the following structure/array which i loaded it from mytext.txt
VarA ParamM ParamN VarB ParamP ParamQ Mode
7000 tot7000 Option1 1.3325 7.09756 7.19919 ER
7000 tot7000 Option2 1.3325 7.09756 7.19919 ER
7000 tot7000 Option1 1.33097 7.09757 7.19919 VR
7000 tot7000 Option2 1.33097 7.09757 7.19919 VR
7000 tot7000 Option3 1.3286 7.09756 7.19919 ER
7000 tot7000 Option3 1.32596 7.09757 7.19919 VR
8000 tot8000 Option2 1.28719 7.09133 7.19268 ER
8000 tot8000 Option1 1.28702 7.09133 7.19268 ER
8000 tot8000 Option2 1.2868 7.09131 7.19267 VR
8000 tot8000 Option1 1.28663 7.09131 7.19267 VR
8000 tot8000 Option3 1.28301 7.09131 7.19267 VR
8000 tot8000 Option3 1.28123 7.09133 7.19268 ER
6000 pop6000 Option1 1.26447 7.10247 7.20513 VR
6000 pop6000 Option1 1.26431 7.10247 7.20512 ER
6000 tot6000 Option1 1.25784 7.10293 7.20541 VR
6000 tot6000 Option2 1.25784 7.10293 7.20541 VR
6000 tot6000 Option1 1.25779 7.10292 7.20540 ER
6000 tot6000 Option2 1.25779 7.10292 7.20540 ER
6000 tot6000 Option3 1.2537 7.10292 7.20540 ER
The idea is to sort and we extract(plot(VarA,VarB) after sorting as follow:
a) Sort rows after Mode (ER for example)
b) Sort the resulting matrix after Param N, say (Option 2)
c) Sort the resulting matrix after Param M say (tot)
Results should be for example:
VarA ParamM ParamN VarB ParamP ParamQ Mode
6000 tot6000 Option2 1.25784 7.10293 7.20541 VR
7000 tot7000 Option2 1.33097 7.09757 7.19919 VR
8000 tot8000 Option2 1.2868 7.09131 7.19267 VR
..........................................................................................
The same for Mode ER
I build a function as follow
function [tot_VR tot_ER]=verif(mydata)
mycell=struct2cell(mydata);
mycell_tab=mycell{2};
vect_ER_VR=mycell_tab(:,7);
%VR
index_VR=find(contains(vect_ER_VR,'VR'));
mycell_tab_VR=mycell_tab(index_VR,:);
vect_VR_Option2=mycell_tab_VR(:3);
index_vect_VR_Option2=find(contains(vect_VR_Option2,'Option2'));
mycell_tab_VR_Option2=mycell_tab_VR(index_vect_VR_Option2,:);
vect_VR_Option2_tot=mycell_tab_VR_Option2(:,2);
index_vect_VR_Option2_tot=find(contains(vect_VR_Option2_tot,'tot)'
mycell_tab_VR_Option2_tot=mycell_tab_VR_Option2(index_vect_VR_Option2_tot,:)
varA_VR=cellfun(@str2num, mycell_tab_VR_Option2_tot(:,1), 'UniformOutput', false);
varB_VR=cellfun(@str2num,mycell_tab_VR_Option2_tot(:,4), 'UniformOutput', false);
pvarA_VR=cell2mat(varA_VR);
pvarB_VR=cell2mat(varB_VR);
tot_VR=[pvarA_VR,pvarB_VR];
......%ER
end
I just wonder if is much easier way how can be done this kind of find, sort and extract?
(I was not using Matlab at all in the last 3 years)
Thank you,
2 Comments
Accepted Answer
Sindar
on 15 Oct 2020
Edited: Sindar
on 15 Oct 2020
tables are a vastly superior way of handling this data. Does this do it?
mydata= readtable('mytext.txt');
sorted_data = sortrows(mydata,{'Modul1';'ParamN';'ParamM'});
4 Comments
Sindar
on 15 Oct 2020
ah, yeah, switching between delimiters could trip up the auto-import (I'm surprised importdata worked). If you always have the same columns, you can deal with it fairly easily. The simplest way is to use the import tool gui and generate a function. It's also possible to code the options (e.g., delimeters = ' ' and ';') directly, but it's a bit of a pain
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!