How can I construct a struct handle from a string?

122 views (last 30 days)
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
Stephen23
Stephen23 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.

Accepted Answer

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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!