How can I construct a struct handle from a string?
Show older comments
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
Walter Roberson
on 27 Oct 2020
I have several structs each named identically but with a numeric at the end
Why? Why didn't you use a single variable that is indexed? How are you getting those struct in memory that could not be done by indexing instead?
Charles Pace
on 27 Oct 2020
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.)
"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.
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!