user3783502
user3783502

Reputation: 3

Allocating matrix / structure data instead of string name to variable

I have a script that opens a folder and does some processing on the data present. Say, there's a file "XYZ.tif".

Inside this tif file, there are two groups of datasets, which show up in the workspace as

data.ch1eXYZ

and

data.ch3eXYZ

If I want to continue with the 2nd set, I can use A=data.ch3eXYZ

However, XYZ usually is much longer and varies per file, whereas data.ch3e is consistent.

Therefore I tried A=strcat('data.ch3e','origfilename'); where origfilename of course is XYZ, which has (automatically) been extracted before.

However, that gives me a string A (since I practically typed

A='data.ch3eXYZ'

instead of the matrix that data.ch3eXYZ actually is. I think it's just a problem with ()'s, []'s, or {}'s but Ican't seem to figure it out.

Thanks in advance!

Upvotes: 0

Views: 47

Answers (1)

sco1
sco1

Reputation: 12214

If you know the string, dynamic field references should help you here and are far better than eval

Slightly modified example from the linked blog post:

fldnm = 'fred';
s.fred = 18;
y = s.(fldnm)

Returns:

y =

    18

So for your case:

test = data.(['ch3e' origfilename]);

Should be sufficient

Edit: Link to the documentation

Upvotes: 3

Related Questions