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 commentaires

KSSV
KSSV le 5 Mai 2022
Why did you name them as run_0, run_1, etc??
Instead, save them into an array.
Stephen23
Stephen23 le 5 Mai 2022
Modifié(e) : Stephen23 le 5 Mai 2022
"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
Gabriele Curcio le 5 Mai 2022
Yes I know that is not an easy task, but regrettably the log of my device is structured as said before.
And at the moment, changing the log structure is harder and slower than trying to find a solution on matlab.
By the way, I did not get your answer.. am I supposed to follow the indications of the function in the link?
Stephen23
Stephen23 le 5 Mai 2022
@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
Gabriele Curcio le 5 Mai 2022
Yes yes, I know it would be easier, but at the moment I am trying to find a workaround to that.
Yes I have imported these variables using LOAD.
Stephen23
Stephen23 le 5 Mai 2022
Modifié(e) : Stephen23 le 5 Mai 2022
"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
Gabriele Curcio le 5 Mai 2022
Ok, I manage to get the expected result! Thank you very much

Connectez-vous pour commenter.

 Réponse acceptée

As mentioned before, this is not the best way to handle your data, BUT you are where you are, so to be able to use the names run_0, run_1, etc you need to concatenate strings:
for i =0:10
name = strcat('run_',num2str(i))
end
name = 'run_0'
name = 'run_1'
name = 'run_2'
name = 'run_3'
name = 'run_4'
name = 'run_5'
name = 'run_6'
name = 'run_7'
name = 'run_8'
name = 'run_9'
name = 'run_10'
Further, do not use i for a counter, i can be used for imaginary numbers, better to use a sensible name like "counterRuns" or something that has a meaning.
Hope this solves your problem

1 commentaire

Stephen23
Stephen23 le 5 Mai 2022
Modifié(e) : Stephen23 le 5 Mai 2022
"...you need to concatenate strings"
A more efficient approach is to use SPRINTF:
sprintf('run_%d',i)
or use the STRING class:
"run_" + i

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Historical Contests dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by