Change variable name in plot
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Gabriele Curcio
le 5 Mai 2022
Modifié(e) : Stephen23
le 5 Mai 2022
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
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.
Réponse acceptée
Constantino Carlos Reyes-Aldasoro
le 5 Mai 2022
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
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!