Accessing to Variables with Incremental Names in a Loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Berk Kucukoglu
el 11 de Abr. de 2019
Comentada: Berk Kucukoglu
el 18 de Abr. de 2021
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 comentarios
Stephen23
el 11 de Abr. de 2019
"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.
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!