AnnaSchumann
AnnaSchumann

Reputation: 1271

Loading specific column from .MAT file using another variable name

How can I load a specific set of data from a .mat file when I need to allow the user to specify which set to import?

For example:

a = 'setII'; % User specifies
db = matfile('example.mat');
model = db.a;

And this will read a as 'setII' and then essentially load db.setII.

Currently it errors as it tries to look for a dataset labelled 'a'.

Upvotes: 0

Views: 204

Answers (1)

MeMyselfAndI
MeMyselfAndI

Reputation: 1320

Use dynamic field references:

model = db.(a)

which works if a is a string that contains the name of a field/property in db.

Example with a struct:

example = struct('name','test','values',[1 2 3 4], 'size', 4);
fieldname = 'values';
x = example.(fieldname)

returns

x = [1 2 3 4]

Upvotes: 2

Related Questions