How can I construct a struct handle from a string?
206 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2020-10-27
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.)
Stephen23
2020-11-20
编辑:Stephen23
2020-11-20
"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.
采纳的回答
Peter Perkins
2020-11-20
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 个评论
Steven Lord
2020-11-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!