From char to double for plotting

2 visualizzazioni (ultimi 30 giorni)
Hello,
I have n=12 "double" data set called I_n. I want a subplot (histogram in this case), for each data set. With the following code the function histogram doesn't recognize the "char" value. How can I better implement this?
figure()
for n=1:12
chartname = strcat('I_',num2str(n));
subplot(3,4,n)
histogram(chartname)
hold on
end

Risposta accettata

Bjorn Gustavsson
Bjorn Gustavsson il 18 Giu 2022
Modificato: Bjorn Gustavsson il 18 Giu 2022
First you should store the data-sets in some kind of array. For the most tolerant storage (that doesn't care about different dimenstions or data-type) use cell-arrays. Then you can do somethinh like this:
figure
for n = 1:12
curr_I = I_all_in_cell{n};
subplot(3,4,n)
histogram(curr_I)
hold on
end
Do not use numbered variables for this type of work, use the different arrays that matlab makes it possible to work with. Just build the array instantly at the point where you load the data. Also look after the numerber of times this type of question have been asked for a more in-depth explanations.
HTH

Più risposte (1)

Image Analyst
Image Analyst il 18 Giu 2022
Do this klunky way:
subplot(3,4,1)
histogram(I_1)
subplot(3,4,2)
histogram(I_2)
subplot(3,4,3)
histogram(I_3)
subplot(3,4,4)
histogram(I_4)
subplot(3,4,5)
histogram(I_5)
subplot(3,4,6)
histogram(I_6)
subplot(3,4,7)
histogram(I_7)
subplot(3,4,8)
histogram(I_8)
subplot(3,4,9)
histogram(I_9)
subplot(3,4,10)
histogram(I_10)
subplot(3,4,11)
histogram(I_11)
subplot(3,4,12)
histogram(I_12)
Now you know why it's not good to have so many uniquely-named variables. You should put them all into a matrix, like @Bjorn Gustavsson suggested, if you can. Then you could use a simple, compact for loop like you wanted.
See the FAQ:

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by