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

Stephen23
Stephen23 2020년 10월 21일
편집: Stephen23 2020년 10월 21일
"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:
Ramo Rafsel
Ramo Rafsel 2020년 10월 23일
Do you happen to have an example how to use one of the alternatives to accessing dynamic variable names? I still am trying with a loop that sorts the table of the variables somewhat automatically but no avail.
thanks a lot in advance for the rescue.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2020년 10월 23일
편집: Stephen23 2020년 10월 23일

1 개 추천

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

Stephen23
Stephen23 2020년 10월 26일
편집: Stephen23 2020년 10월 26일
@Omar Lesfar : in your original question you stated that "...there are 40 Variables that I want to plot", so lets just focus on doing that. It appears that your data was obtained by some kind of event-driven logging, which means the different "Variable" values occur with different/irregular time steps. But the data are already sorted by timestamp, so you do not need to sort the data (but you could easily by using sortrows).
For plotting you can simply loop over the "Variable" values, e.g.:
D = './subdir';
mkdir(D) % only required once
S = load('matlabtable.mat');
T = S.HV801000front10Vml2;
U = unique(T.Variable);
for k = 1:numel(U)
X = T.Variable==U(k);
plot(T.time(X),T.Value(X))
xlabel('time')
title(char(U(k)),'Interpreter','none')
F = sprintf('%s.png',char(U(k)));
saveas(gcf,fullfile(D,F))
end
The first image looks like this (I will not post the other 73 images):
Stephen23
Stephen23 2020년 10월 26일
편집: Stephen23 2020년 10월 26일
"Maybe there is another or a short way that does the same thing without using a code with over 150 lines?"
Yes, use a loop just like I showed you in my answer. Every time you create lots and lots of variables with names like E1_ABD_FoamEventCnt you are forcing yourself into writing lots and lots and lots of code. Basically bad data design (lots of separate variables) forces you into writing bad code (lots of copy-paste). Avoid doing that.
"But I am trying to sort the table I added by the name of the tables"
If you look at my last comment the variable U is sorted, and the loop processes them in that order. You could easily add an explicit list for U if you only want a subset of the "Variable" values:
U = {'E1_ABD_FoamEventCnt','E1_LD_ActualStatus',..}
or use some string matching to only parse the "Variable" values that you want (e.g. strncmp, contains, etc.).
"I did this manually, these are the tables that I wanted to be extracted from the original one..."
You can easily split up that table and store them in a cell array:
tblA = sortrows(Table1,{'Variable','Value'});
out = cell(1,40) % preallocate
for k = 1:40
vnm = sprintf('Variable %d',k);
idx = tblA.Variable==vnm;
tbl = tblA(idx,:);
... your code
out{k} = tbl;
end
Although most likely that is not a particularly good use of the table class, which is designed for processing groups of data within one table.
Ramo Rafsel
Ramo Rafsel 2020년 10월 26일
편집: Ramo Rafsel 2020년 10월 26일
Thanks you so much for the detailed answer and explanation @Sephen Cobeldick. I see what you mean. I'll re-read everything you wrote here in case I missed an information.
The code works and the line about the U is the thing that bothered me actually the whole time, using and plotting only specific variables that I wanna use.
I am thankful for you answer and your great help!
Stephen23
Stephen23 2020년 10월 26일
편집: Stephen23 2020년 10월 26일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 10월 21일

편집:

2020년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by