Is it possible to use the LOAD function to load structure fields selectively in MATLAB?

7 Ansichten (letzte 30 Tage)
I would like to load structure fields selectively in MATLAB using the LOAD function. For example, if I define the following structure:
a.x = 1;
a.y = 2;
save example a;
It is possible to selectively load the structure variable a:
load example a
I want to be able to selectively load only the x field of a.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 15 Mai 2018
Use the "-struct" option when saving MAT-files so that structure fields are saved as separate variables. You can then use the LOAD function to select particular variables from the MAT-file:
a.x = 1;
a.y = 2;
save('example','-struct','a');
clear all;
load('example','x');
The "-struct" option is not available prior to MATLAB 7.0 (R14).
Using the "struct" flag causes each field of the structure to be individual variables in the MAT-file. To load them back into a structure use:
>> a = load('example')
  5 Kommentare
K E
K E am 5 Mai 2016
Bearbeitet: K E am 5 Mai 2016
A downside of the -struct flag is that the *.mat file contains a set of variables so I lose the original structure, unless I am missing something.
Richard Crozier
Richard Crozier am 15 Mai 2018
@K E, the advantage is that if you then use load on the same file like
S = load (filename)
It's put back into the structure again. In fact there's not a simple way to load variables from a file into a variable directly without them ending up in a structure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by