Load a field of struct into a variable (MATLAB)
11,357
Solution 1
You can't load part of a variable from a MAT-file. You want either:
var = load( fullfile(path, 'PRICES.Mat'), 'PRICES' );
var = var.PRICES.Raw;
or
load( fullfile(path, 'PRICES.Mat'), 'PRICES');
var = PRICES.Raw;
See MATLAB help: http://www.mathworks.co.uk/help/techdoc/ref/load.html
Solution 2
If you are using MATLAB 7 or higher, you can save your struct using the -struct
flag:
save(fullfile(path, 'PRICES.Mat'),'-struct','PRICES');
If you save your struct this way, then you can load a specific field of the struct without loading all of the struct's fields:
load(fullfile(path, 'PRICES.Mat'),'Raw');
disp(Raw);
Author by
Maddy
Updated on June 04, 2022Comments
-
Maddy almost 2 years
I have a struct stored onto my harddrive. I need to load one of its Field into a local variable. A simple load gets the
% 'PRICES' is the stored struct. 1st fieldname is '.Raw'. % Only '.Raw' needs to be loaded var = load( fullfile(path, 'PRICES.Mat') ) % Wrong as var becomes a struct containing a struct. % DESIRED value: var = PRICES.Raw ;
Is it possible to do this in 1 step? I can ofcourse overwrite var and accomplish this, but is there a direct way of doing it? Thanks.
-
KAE almost 8 yearsBut then the *mat file contains a bunch of variables, not a structure any more, right?
-
Khoa over 7 yearsWell, is there an easy way to reconstruct the original struct from the decomposed variables if one whole struct is needed? Otherwise, we just need to decide upfront what we really want to load.
-
Khoa over 7 yearsAlso, "the argument to -STRUCT must be a scalar structure variable." So if we have an array of structs, this will fail.