Change variable name in plot
이전 댓글 표시
I have these variables as a double:
time; run_0; run_1; run_3; ...; run_23
how can I plot them without rewriting everytime the function plot?
The idea is:
for i = 0:23
plot(time, run_i); %index 'i' to change dynamically
hold on
end
I do not know how to write the index 'i'.
댓글 수: 7
KSSV
2022년 5월 5일
Why did you name them as run_0, run_1, etc??
Instead, save them into an array.
"I do not know how to write the index 'i'."
That is not an index, it is a pseudo-index which is part of the variable name, which makes it much harder to process your data. Acessing variable names dynamically is slow, complex, inefficient, and best avoided.
If you had used an actual index (as you should have) then this task would be very simple and efficient:
Gabriele Curcio
2022년 5월 5일
Stephen23
2022년 5월 5일
@Gabriele Curcio: how did you import this data into the MATLAB workspace? Most likely that is the best place to handle this situation. For example, did you import those variables using LOAD ?
" I did not get your answer.. am I supposed to follow the indications of the function in the link?"
The link shows you how simple this task would be if you used indexing.
Gabriele Curcio
2022년 5월 5일
"Yes I have imported these variables using LOAD."
Good, now we are getting somewhere. Always LOAD into an output variable:
S = load(..)
All of the loaded variables are fields of the scalar structure S. Note that you can easily obtain the fieldnames
C = fieldnames(S)
and then loop over them (or just generate them yourself e.g. using SPRINTF) and use dynamic fieldnames, for example to access the 2nd field of the structure:
S.(C{2})
How many variables are in each MAT file? (Or are you unfortunately using LOAD to import textfiles?)
Please upload your data files (two or three, not all) by clicking the paperclip button.
Gabriele Curcio
2022년 5월 5일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Historical Contests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!