From char to double for plotting
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bjorn Gustavsson
2022-6-18
编辑:Bjorn Gustavsson
2022-6-18
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 个评论
更多回答(1 个)
Image Analyst
2022-6-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!