Sort and extract elemets from a structure.

1 回表示 (過去 30 日間)
Ionut  Anghel
Ionut Anghel 2020 年 10 月 14 日
コメント済み: Sindar 2020 年 10 月 15 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 10 月 14 日
Please attach mytext.txt and the code to read it in to your structure.
Ionut  Anghel
Ionut Anghel 2020 年 10 月 14 日
mydata= importdata('mytext.txt')

サインインしてコメントする。

採用された回答

Sindar
Sindar 2020 年 10 月 15 日
編集済み: Sindar 2020 年 10 月 15 日
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 件のコメント
Ionut  Anghel
Ionut Anghel 2020 年 10 月 15 日
編集済み: Ionut Anghel 2020 年 10 月 15 日
I use R19.
A row of this in the real mytext.txt file looks like:
2341;5700;AAAmod;tot5700;Option1;1.32787;1.18734;0.14053;1.18624;-;7.13773;7.27830;7.24058;3.65;none;NoAPC_pum.041013;BE;VR;-;-;1;1.0;1.8303;2.288;1.4578;4.08026;4.1811;3.9675;BE;BE;fff629;mp912;-v 6.9.5.2;B2Lossof.200811;ons okt 14 23:57:36 CEST 2020;;;;BE;0;0
This is one row with 41 columns.
My file has 100 rows i.e dimension is 100x41.
When i order readtable the outpput is 99X7 instead of 100x41.
And I have over 100 files like this :)
Sindar
Sindar 2020 年 10 月 15 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by