2

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
Maddy
  • 2,520
  • 14
  • 44
  • 64
  • What do you mean by a "field" of a MAT-file? Do you mean "variable", or "field of a variable"? – Nzbuu Aug 31 '11 at 16:44
  • PRICES was a struct before saving with the first fieldname as 'Raw'. After `save` command, it was stored as PRICES.Mat. I need to rerieve the PRICES.Raw matrix and feed it into a variable. – Maddy Aug 31 '11 at 16:47

2 Answers2

8

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);
Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
  • But then the *mat file contains a bunch of variables, not a structure any more, right? – KAE May 05 '16 at 17:48
  • Well, 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 Aug 11 '16 at 04:22
  • Also, "the argument to -STRUCT must be a scalar structure variable." So if we have an array of structs, this will fail. – Khoa Aug 11 '16 at 11:39
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

Nzbuu
  • 5,241
  • 1
  • 29
  • 51