How to set() plot in matlab with cell array

27 ビュー (過去 30 日間)
m j
m j 2020 年 1 月 16 日
編集済み: m j 2020 年 1 月 17 日
Hello, ive been trying to set a plots xAxis and yAxis so it plots my cell array.
cellArray = {1,4}. cellArray has 4 cells and each cell is a 1024X1 array.
Ive tried
set(hPlot3, 'Xdata',1:1024,'Ydata',[cellArray(:)]);
And tried setting cell array to matrix and plotting:
A = cell2mat(cellArray);
set(hPlot3, 'Xdata',1:1024,'Ydata',A);
But both have error'Value must be a vector of numeric type'.
I tried looking up setting a plot with cell arrays involved but didnt find anything.
I know this is simple fix but am having a very hard time with it.Apologies.
using this allows me to plot 1 cell but not all:
set(hPlot3, 'Xdata',1:1024','Ydata',cellArray{1,1}(:));

回答 (2 件)

Amit
Amit 2020 年 1 月 16 日
This should work, I think you had missed the transpose of x-array:
A = cell2mat(cellArray);
set(hPlot3, 'Xdata',1:1024','Ydata',A);
  1 件のコメント
m j
m j 2020 年 1 月 17 日
編集済み: m j 2020 年 1 月 17 日
Unfortunantly not.....
I get error:
Error using matlab.graphics.chart.primitive.Line/set
Value must be a vector of numeric type
Seems like it should work since I set x axis of (1:1024) to same length of x axis in cellArray....

サインインしてコメントする。


Walter Roberson
Walter Roberson 2020 年 1 月 17 日
You cannot do that. When you have multiple lines to plot then each of them must have its own line object
h = plot(x, y_with_multiple_columns)
will give you back an array of line objects, one for each column of y. You would then set() the YData of each of the handles individually.
There is an obscure way to set multiple properties for multiple handles, but you probably should not be using it.
  1 件のコメント
m j
m j 2020 年 1 月 17 日
編集済み: m j 2020 年 1 月 17 日
Ahh thanks but, I found a way from an older post that allows me to do what I want.
A = cell2mat(cellArray);
t= 1:1024;
y1=A(:,1);
y2=A(:,2);
y3=A(:,3);
subplot(2,2,1);
plot(t,y1);
hold on
plot(t,y2,'r');
plot(t,y3,'b');
hold off
Let me know if im doing a matlab cardinal sin by doing it this way.It works,lets me plot multiple data sets on 1 subplot. But so it seems using 'hold on' retains plot and the subplot its using. So if I wanted to update a different subplot I would do same,just different subplot?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by