Non Scalar Structures

Hi how can I save a non scalar structures. Again, I don't really know what a nonscalar structure is. I know mine is a nonscalar structure because save('filename',-struct,'structname) throws exception saying your structure is nonscalar.
I am trying to save a matrix of structures each structure has a structure inside it. does such programming increase runtime?

Risposte (2)

Jan
Jan il 16 Feb 2012
No, such programming does not necessarily increase the runtime. It can be a nice and efficient structure to represent the data. Even if it runs some seconds slower, a clear data representation can save weeks of debugging.
A non-scalar struct is something like this:
S(1,1).a = 1;
S(2,1).a = 2;
S(1,2).a = 3;
S(2,2).a = 4;
Now you have a [2x2] struct with the field 'a'. You cannot save this using the '-struct' flag, but by:
save('filename.mat', 'S');

6 Commenti

Pankaj
Pankaj il 16 Feb 2012
hi Ian , thanks for reply.
I tried using Save without -struct tag but it says it can only save string format. can i convert my struct to a string ?
Jan
Jan il 16 Feb 2012
Please post the command, which causes the error message. The code I've posted does work.
Malcolm Lidierth
Malcolm Lidierth il 16 Feb 2012
The documentation uses 'scalar structure' unclearly in places. You might expect a scalar structure to be any structure for which isscalar returns true. But, for example when using loadlibrary, the MATLAB docs say a scalar structure is required - but that means a scalar structure with all its fields also scalar (at least it did a few years ago).
Walter Roberson
Walter Roberson il 16 Feb 2012
When you use the -struct flag of save(), the structure itself must be of length 1, only S(1) defined and not S(2) or so on.
The -struct flag is a useful construct to "undo" the effect of loading a .mat file in to a single structure by way of
S = load('YourFile.mat');
The resulting S would be a scalar structure.
If you are not trying to do the opposite of load() in to an output variable then you should probably not be using the -struct flag of save(); it is not needed to save general structures.
Pankaj
Pankaj il 19 Feb 2012
Hi Jan, sorry for late reply.
The code you posted works. I was doing a mistake passing S without single quotes. The issue however is, that this still doesn't solve the problem. It takes Matlab more than half an hour to save the variable.The size of the structure in Bytes is close 5.75 GB.
Any suggestions? thanks
Jan
Jan il 19 Feb 2012
Writing 5.75 GB to a disk takes some time. Do you write the MAT file in the -7.3 format (see "help save", if this is not clear)? You cannot use the faster -v6 flag for SAVE, because even the compressed file is larger than 2GB already.
If your staruct has a lot of elements (millions), consider that each element has more than 100 Bytes overhead. E.g.:
[S(1:1e4).a] = deal(1); T.a(1:1e4) = 1; whos
You have 1200064 Bytes for S and 80176 bytes for T. A struct of fields consumes less memory than a field of structs. This matters the requirements of RAM, of disk-space when using SAVE and even the processing speed depends on the internal data representation.

Accedi per commentare.

upol
upol il 10 Gen 2019

0 voti

Why this gives problem in C Coder. Error: Directly accessing field or property of nonscalar struct or object not supported for code generation.
s1=string({OPS_FLT(:).ACFT_ID})
s2=OPS_FLT(2).ACFT_ID
uuindex=find(strcmpi(s1,s2))
({OPS_FLT(:).ACFT_ID}) has already been defined as
OPS_FLT(1).ACFT_ID="apple"
OPS_FLT(2).ACFT_ID="orange"
I am trying to find orange from the array. It works in Matlab but not in C Coder

Prodotti

Richiesto:

il 16 Feb 2012

Risposto:

il 10 Gen 2019

Community Treasure Hunt

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

Start Hunting!

Translated by