Matlab structure of struct
8 次查看(过去 30 天)
显示 更早的评论
Hi!
I'd like to create structures and store them, but I don't know how to do it. For example, for a function, we write something like
function f=MyFunction(arg1,...,argN)
...
end
and then we write the different operations. This allows us to write MyValue=MyFunction(...). I'd like to do the same for structures, something like MyObject=MyStruct(...). Could someone give me a reference document which explains how to do this for structures please? So far, the only thing I found is how to create structures from the command window. Thanks!
2 个评论
Jan
2021-11-12
Stucts can be defined in the code exactly as they are created in the command window. See:
doc struct
doc cell2struct
doc table2struct
采纳的回答
Jan
2021-11-12
MyStruct = cell2struct(cell(2, 1), {'field1'; 'field2'})
Now the fields exist, but contain empty matrices only.
更多回答(1 个)
Image Analyst
2021-11-12
Not sure what you mean. You can call the function whatever you want and it can take in and return whatever you want. If you want to create a custom structure, there is already a function called struct() to do that.
5 个评论
Image Analyst
2021-11-12
Looks like Jan showed you how to create one struct. If you want an array of them, call repmat():
MyStruct = cell2struct(cell(2, 1), {'field1'; 'field2'})
structArray = repmat(MyStruct, 5, 2 )
structArray will be a 5 by 2 array of structures.
If that helps, can you "Vote" for my answer?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!