Search code examples
matlabvariablesstructmatlab-load

Load a field of struct into a variable (MATLAB)


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.


Solution

  • 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