开发者

Load a field of struct into a variable (MATLAB)

开发者 https://www.devze.com 2023-04-02 01:56 出处:网络
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

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 o开发者_运维技巧verwrite var and accomplish this, but is there a direct way of doing it? Thanks.


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消