2

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.

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that solves my problem if I have only S(1) to S(3) or so. However in my case there is 280 'S' due to my structure having 280 of that. How do I go about filling those S(3) to S(280) point1 and point 2 automatticaly? Thanks
@Melvyn my answer just gives an example. The same line (i.e., [S.point1]) will work perfectly fine even when S contains a billion values. Did you try the above on your own structure?
sorry, so do I change "S" into my own cell array name? I.E after struct2cell, i renamed the cell array as "cellData". This array has 280 point1 and point2. So do I write [cellData.point1]?
@RodyOldenhuis You might have some memory problems with a billion values. :)
@Doresoom conceptually speaking, of course :) BTW, 1e9 values is about 8GB == no problem at all on my machine :p
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.