Plot cell array without loop

微笑、不失礼 提交于 2019-12-11 14:03:17

问题


I have the following code:

SimRun = 0
Count = 0

for b = 1:0.5:3000
.
.
.
.
    Count = Count + 1;
    ArrayT(Count) = Time;
    ArrayTgo(Count) = tgo;
    ArrayY(Count) = Y;
    ArrayYD(Count) = YD;

end

SimRun = SimRun  + 1;
MAT_ArrayT{SimRun,:} = ArrayT;
MAT_ArrayTgo{SimRun,:} = ArrayTgo;
MAT_ArrayY{SimRun,:} = ArrayY;
MAT_ArrayYD{SimRun,:} = ArrayYD;

As you can see, I have 2 for loops. From the inner loop I receive a vector and from the outer loop I receive in the end a cell array where each cell is a vector.

Now, I had like to plot the cell array to plot basically around 6000 lines and I did it as follows:

for i = 1:SimRun

    figure(1)
    hold on
    plot(MAT_ArrayT{i,:},MAT_ArrayY{i,:})

    figure(2)
    hold on
    plot(MAT_ArrayT{i,:},MAT_ArrayYD{i,:})

end

However this solution takes pretty much time to draw all the lines.

Is there any better solution to store "lines" and plot all of them in one hit at the end?

Thank you.


回答1:


The plot command takes a matrix and plots each column as a separate line. Assuming you have column vectors in your cell arrays, and they're all the same length, you can do this:

x = [MAT_ArrayT{:}];
y = [MAT_ArrayY{:}];
plot(x,y)

Even better would be to store those vectors in a numeric matrix to start with, so you don't need to make the extra copy.



来源:https://stackoverflow.com/questions/48331371/plot-cell-array-without-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!