Plot data from a cell array in a struct

조회 수: 2 (최근 30일)
Clément P
Clément P 2016년 4월 12일
편집: Clément P 2016년 4월 12일
Hi everyone,
I want to plot (in a GUI) datas from a cell array. This cell array is contained in a structure, and this structure is stored in the handles.
I have something like:
handles (1x1) > Struct (1xhandles.n) > Data (XXXX*XX cells)
I have a lot of data and I want to plot some columns. I tried this :
for i=1:handles.n
handles.plot(i)=plot(handles.struct(i).data{:,handles.colum+3},handles.struct(i).data{:,handles.colum+1});
end
And this
for i=1:handles.n
handles.plot(i)=plot([handles.struct(i).data{:,handles.colum+3}],[handles.struct(i).data{:,handles.colum+1}]);
end
Both are not working. I get this error msg :
Error using plot
Invalid first data argument
I guess plot can't access values inside the datas cell array.
I succeeded doing this plot by creating a matrice, which store data and then it is quite easy to plot.
for i=1:handles.n
for j=1:lentgh()
X(j,i)=handles.struct(i).data{j,handles.column+3};
Y(j,i)=handles.struct(i).data{j,handles.column+1}
end
handles.plot(i)=plot(X(:,i),Y(:,i));
end
This one works but I'm sure there is a better way of doing this, and a faster too.
Feel free to answer ! Thx.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 12일
This is not clear
Clément P
Clément P 2016년 4월 12일
편집: Clément P 2016년 4월 12일
I want to plot certain columns of my cell array called Data. Which is stored in a structure (struct), itself stored in the handles of my GUI (handles).
I succeeded by creating a matrice, but I want to know if there is a simpler/more efficient way of doing this, beacause I have a huge amount of data.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 4월 12일
I would recommend you use a table instead of a cell array. It makes manipulating heterogeneous storage easier.
I get the 'invalid first data argument' error if I pass strings to plot. Are you sure that the data in the columns you want to plot is numeric?
Note that your first syntax is certainly not going to work, but your second should, assuming the data in column + 3 and column + 1 is all scalar or row vectors. Otherwise this will work:
%note that handles.n is probably relevant and should be the same as
%numel(handles.struct)
%in which case get rid of the variable.
for plotnumber = 1 : numel(handles.struct)
handles.plot(plotnumber) = plot(cell2mat(handles.struct(plotnumber).data(:, handles.column + 3)), ...
cell2mat(handles.struct(plotnumber).data(:, handles.column + 1)));
end
Note that if all the cell arrays are the same size, it's probably possible to get rid of the loop.
  댓글 수: 1
Clément P
Clément P 2016년 4월 12일
편집: Clément P 2016년 4월 12일
Actually I made a ridiculous mistake... I forgot the titles in my cell array, which is why I had the 'invalid fisrt data argument'. Thanks for your answer anyway, I have still learned something ! ;)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by