MATLAB Answers

How can I construct a struct handle from a string?

2 views (last 30 days)
Charles Pace
Charles Pace on 27 Oct 2020
Commented: Steven Lord on 20 Nov 2020
I am encountering a lot of answers about the evils of dynamicly assigning variable/funciton names, etc. As a new user, I am trying to learn the right way to use MATLAB, and am trying to heed to lessons found in MATLAB answers.
However, at present, I have several structs each named identically but with a numeric at the end and I am working within a limited time frame. I am hoping to be able to assign a string to match the name of the struct and then turn the string into a handle that lets me index into/access values within each of the structs. Is there a simple way about this?
example struct names:
'structData1'
'structData2'
I want to ideally say:
for ii = 1: (10)
sname = ['structData', ii]
% Convert string 'sname' to struct in memory
data_of_interest = sname.fieldname
Usin 'eval' seems like a pain. Is there an easier way? What are structs essentially? What data type would I be converting the string value to in order to access the place in memory where a struct is stored?

  4 Comments

Show 1 older comment
Charles Pace
Charles Pace on 27 Oct 2020
I am not sure. It seemed convenient to store the data in a stuct , becase it sort of resembled a table format.
1 of the fields corresponds to one of the params used to generate the matrices stored in the other fields. It helps me to be able to look at the struct like a table.
The columns of the struct are like so { Param, Matrix, Matrix, Matrix ... }
The matrices are of different dimensions but relate/were generated by the parameter.
Are you saying I should have just saved the matrices each in their own variable, and then via indexing and smart-variable naming, been aware of which parameter they corresponded to?
I'm sorry. I am new to MATLAB and structs seemed like a convenient tablature way to format the data. Maybe a cell array is better? I don't know! That's why I am asking lol.
Walter Roberson
Walter Roberson on 27 Oct 2020
MATLAB has tables which are mostly aimed at 2d work (rows each with a value for each named variable)
MATLAB structures can be arrays. When that is used, each array element must have the same field names, but the types and sizes stored do not need to be consistent.
When the datatypes of a collection of objects need to differ or named fields are not appropriate, then cell arrays can be used.
Structures can also have dynamic field access
Variable.(expression)
where expression evaluates to a field name. For example if you can encode your file name as a valid matlab variable name then you can effectively get a structure with fields named after the files. This is workable, but not quite as clean as keeping a variable listing the file names, searching it to find the index of a given file name, and using that index to index a structure array or cell array (those permit easier processing of all the stored information since if you need to (say) normalize all of the data you do not care what its associated name is.)
Stephen Cobeldick
Stephen Cobeldick on 20 Nov 2020
"I have several structs each named identically but with a numeric at the end and I am working within a limited time frame."
A limited time frame... and poor data design that slows you down with problems that you cannot solve by yourself.
"I am hoping to be able to assign a string to match the name of the struct and then turn the string into a handle that lets me index into/access values within each of the structs. Is there a simple way about this?"
No.
Poor data design cannot be fixed by some "simple way". That why it is considered bad data design.
"I am not sure. It seemed convenient to store the data in a stuct , becase it sort of resembled a table format."
You missed the point of Walter Roberson's comment. There is no problem with using structures. The problem is numbering variables and then hoping to find a simple, efficient, neat, easy way to access them dynamically.

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 20 Nov 2020
A scalar struct with a field for each variable that you would have put in the workspace is often useful. It's like a private workspace, but one in which you can get at things with "dynamic" names with having to resort to eval.
s.(myVarName)
You can also use structfun to loop over every field.

  1 Comment

Steven Lord
Steven Lord on 20 Nov 2020
One way to quickly turn the independent struct arrays you have in the workspace into one large struct, with each of the independent structs as fields, is to use save and load as long as you have a directory to which you have write permission (like tempdir).
>> cd(tempdir)
>> s1 = struct('foo', 42, 'bar', -99);
>> s2 = struct('abracadabra', "magic word");
>> s3 = struct('huey', [1 0 0], 'louie', 'g', 'dewey', 'blue');
>> whos
Name Size Bytes Class Attributes
s1 1x1 368 struct
s2 1x1 342 struct
s3 1x1 562 struct
>> save temporaryData.mat
>> data = load('temporaryData.mat')
data =
struct with fields:
s1: [1×1 struct]
s2: [1×1 struct]
s3: [1×1 struct]
>> data.('s3').dewey
ans =
'blue'
But I agree with the rest of the group -- avoiding creating the variables s1, s2, s3 independently is preferable IMO.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!