Change variable name in plot

3 views (last 30 days)
Gabriele Curcio
Gabriele Curcio on 5 May 2022
Edited: Stephen23 on 5 May 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
I do not know how to write the index 'i'.
Gabriele Curcio
Gabriele Curcio on 5 May 2022
Ok, I manage to get the expected result! Thank you very much

Sign in to comment.

Answers (1)

Constantino Carlos Reyes-Aldasoro
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))
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 Comment
Stephen23 on 5 May 2022
" need to concatenate strings"
A more efficient approach is to use SPRINTF:
or use the STRING class:
"run_" + i

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by