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

230 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 May 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')
  6 Comments

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!