From char to double for plotting
2 views (last 30 days)
Show older comments
Nicolò Monaco
on 18 Jun 2022
Edited: Bjorn Gustavsson
on 18 Jun 2022
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
0 Comments
Accepted Answer
Bjorn Gustavsson
on 18 Jun 2022
Edited: Bjorn Gustavsson
on 18 Jun 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
0 Comments
More Answers (1)
Image Analyst
on 18 Jun 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:
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!