A loop for sorting tables
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a long excel file that contains values and times of different variables,which I already sorted by the variable name into different tables (the code below).
Since there are 40 Variables that I want to plot. I would like to know if it is possible to write the 5 lines only once and loop it for each and every variable?
thanks a lot for the help!
tblA = sortrows(Table1,{'Variable','Value'});
Variable_1 = tblA(tblA.Variable == 'Variable 1', :);
Variable_1.time = (seconds(Variable_1.time)/(1000));
Duration_Variable_1=Variable_1.time;
Duration_Variable_1.Format='hh:mm:ss';
Variable_1.time = round(Duration_Variable_1);
Variable_2 = tblA(tblA.Variable == 'Variable 2', :);
Variable_2.time = (seconds(Variable_2.time)/(1000));
Duration_Variable_2=Variable_2.time;
Duration_Variable_2.Format='hh:mm:ss';
Variable_2.time = round(Duration_Variable_2);
Variable_3 = tblA(tblA.Variable == 'Variable 3', :);
Variable_3.time = (seconds(Variable_3.time)/(1000));
Duration_Variable_3=Variable_3.time;
Duration_Variable_3.Format='hh:mm:ss';
Variable_3.time = round(Duration_Variable_3);
Variable_4 = tblA(tblA.Variable == 'Variable 4', :);
Variable_4.time = (seconds(Variable_4.time)/(1000));
Duration_Variable_4=Variable_4.time;
Duration_Variable_4.Format='hh:mm:ss';
Variable_4.time = round(Duration_Variable_4);
Variable_5 = tblA(tblA.Variable == 'Variable 5', :);
Variable_5.time = (seconds(Variable_5.time)/(1000));
Duration_Variable_5=Variable_5.time;
Duration_Variable_5.Format='hh:mm:ss';
Variable_5.time = round(Duration_Variable_5);
.
.
.
.
2 Commenti
Stephen23
il 21 Ott 2020
Modificato: Stephen23
il 21 Ott 2020
"Since there are 40 Variables that I want to plot..."
Then bad data-design will force you into slow, complex, inefficient, buggy code to access your data:
Numbering variables like that is a sign that you are doing something wrong. The simple and efficient approach would be to use indexing (in which case your question is trivially answered: yes, by using a loop and indexing).
"I would like to know if it is possible to write the 5 lines only once and loop it for each and every variable? "
I think Steven Lord can answer that best:
Risposta accettata
Stephen23
il 23 Ott 2020
Modificato: Stephen23
il 23 Ott 2020
tblA = sortrows(Table1,{'Variable','Value'});
for k = 1:40 % or whatever the max 'variable' is.
vnm = sprintf('Variable %d',k);
idx = tblA.Variable==vnm;
tbl = tblA(idx,:);
tbl.time = seconds(Variable_1.time)/1000;
tmp = tbl.time;
tmp.Format='hh:mm:ss';
tbl.time = round(tmp);
... whatever else you want to do, e.g. assign back into the original table:
tblA(idx,:) = tbl;
end
There are probably better ways to do this, e.g. using retime or dateshift instead of converting to seconds and rounding. But without your data, I cannot test any of this.
4 Commenti
Stephen23
il 26 Ott 2020
Modificato: Stephen23
il 26 Ott 2020
@Omar Lesfar: I hope that it helps! Writing good MATLAB code sometime just requires a slightly different way of looking at a problem... and changing our view of something is often the hardest part!
The volunteers on this forum are always happy to help if you have more questions, or want some examples, etc.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!