Melvyn
Melvyn

Reputation: 41

Can't store values into array MATLAB

I have a 1x280 structure in Matlab. In order for me to use the values, I changed it to struct2cell array.

An example of how one of the structures among the 280 looks is below :

Field       Value               Min       Max

point1      [29,469]            29        469
point2      [42,469]            42        469
-------------------------------------------

After changing to a cell array using the below code:

showlines = struct(lines);
cellData = struct2cell(showlines);


cellData{1,1}(1) 
    = 29

However, if I use this :

cellData{1,1:280}(1);

There is an error

Error:: bad cell reference operation

I would need to keep all the x values of point1 in each of the 280 structures into an array so as to find out the maximum X value of point1 in them. Any idea how to do it?

Thank you very much in advance.

Upvotes: 2

Views: 337

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

Although not a direct answer to your question, you might be interested to know that

%# some example data
S(1).point1 = [29 469];
S(1).point2 = [42 469];

S(2).point1 = [30 470];
S(2).point2 = [43 470];

...

S(280).point1 = [130 870];
S(280).point2 = [243 970];

%# transform to regular array
pt1 = reshape([S.point1],[],2).';
pt2 = reshape([S.point2],[],2).';

will result in

pt1 = [29   469       pt2 = [42   469
       30   470              43   470
       ...                   ... 
       130  870];            243  970];

which enables you to do things like

>> pt1(:, 2)
ans = 
    469
    470
    ..
    870

>> min(pt1(:,1))
ans = 
    29

Does that solve your problem?

To any passers by: what is the notation [S.field] for non-scalar structs called? Does it even have a name? Questions involving this technique frequently pop-up, and it would help if I knew what it's called so I can post a link to the manual page in the answer...

Upvotes: 1

Related Questions