Accessing to Variables with Incremental Names in a Loop
1 次查看(过去 30 天)
显示 更早的评论
Hello,
In my workspace, I have the data structures:
stream1 (1x1 struct)
stream2 (1x1 struct)
...
streamN (1x1 struct)
I would like to create a master struct, merging all of these. I thought of using a "for loop" for this job.
I have also read some other questions, having extremely similar topics. In all of those questions, the asker was warned about not using this kind of "dynamic variable naming" kind of stuff. People wrote lines and lines of text telling how bad of a practice it is.
However,
I need to do this. The device I own gives data in this form - in the form of streams. There is no way I can merge them one by one. I need to reach them in a loop.
Hence my question is:
How to do this? Or, is there any other way to do this?
Thanks,
Berk
4 个评论
Stephen23
2019-4-11
"I need to do this."
I doubt that.
"The device I own gives data in this form - in the form of streams."
Sure. But you forgot to tell us the one vital piece of information which would let us actually help you: how did this data get into the MATLAB workspace? Most likely you did not sit and write all of those variable names out by hand, which means that you likely imported that data from file/s, or the data was generated by some badly written third-party code. In either case, that is where you should fix your code: at the source of the problem, not by dynamically accessing variable names.
For example you might be able to avoid the whole thing by simply loading into an output variable:
S = load(...)
and accessing the fields.But it depends on how that data gets into the MATLAB workspace which you have forgotten to tell us anything about.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!