File names in plots legend, from struct data; Matlab says is not an array.

조회 수: 8 (최근 30일)
I have multiple files with struct data, and I want to place a legend with a group of data.
I tried to use the Benjamin Kraus to solve it, but the message of Matlab is: Cell contents assignment to a non-cell array object.
https://www.mathworks.com/matlabcentral/answers/81979-an-elegant-alternative-to-nested-for-loops-for-plotting-simulations The code which I wrote:
for i = 1:numreg
leg{i} = [BLR4.record{i}.name];
C1 = BLR4.record{i}.T;
C2 = BLR4.record{i}.Sd;
plot (C1,C2)
hold on
end
legend(leg)
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 28일
편집: Stephen23 2018년 9월 28일
[BLR4.record{i}.name]
If BLR4.record{i} is a scalar structure, then those square brackets are superfluous:
Note also that using a non-scalar structure might be better than a cell array of scalar structures:

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

채택된 답변

Stephen23
Stephen23 2018년 9월 28일
Probably leg is not a cell array, so trying to use cell array indexing with it is going to be an error.
Solution: make leg a cell array:
leg = cell(1,numreg);
for ...
leg{k} = ...
...
end
  댓글 수: 8
Stephen23
Stephen23 2018년 9월 28일
편집: Stephen23 2018년 9월 28일
In your code you define numreg like this:
numreg = size(BLR4.record);
which means that numreg will be a numeric vector with atleast two elements (i.e. a vector). But to use cell (and colon reliably) you will need to use a scalar. Most likely you want the number of rows, so you can simply specify that you only want that single value (i.e. scalar) when you call size:
numreg = size(BLR4.record,1);
Isai Fernandez
Isai Fernandez 2018년 9월 28일
@Stephen Cobeldick Yeah. Thank you so much, that was it.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by