How to name variables in a loop?
8 次查看(过去 30 天)
显示 更早的评论
I have 3 structures with name
Time1, Time2 and Time3 but I want to rename them in a loop such that it can be rennamed such as
A.Time1,B.Time2,C.Time3
and then within Time1, Time2 and Time3 there are 2 vectors each i.e., Time1.v1 and Time1.v2; Time2.v1 and Time2.v2; Time3.v1 and Time3.v2 respectively. I also want to rename them in a loop as
A.Time1.v1 and A.Time1.v2; B.Time2.v1 and B.Time2.v2; C.Time3.v1 and C.Time3.v2
7 个评论
Stephen23
2022-6-4
编辑:Stephen23
2022-6-4
"so for plots say I want to plot"
Aaah, so your question is an example of this:
Rather than telling us about your attempted solution using ugly dynamic variable names, you should explain your actual task/goal/problem that you are trying to solve.
"I am asking this as I have to generate 100 of plots and I cannot repeat the code"
Why not? Use indexing. Every array can be accessed using indexing, including container arrays (e.g. tables, cell arrays and structure arrays). What is stopping you from storing your data in arrays and using indexing?
采纳的回答
Stephen23
2022-6-4
Use a cell array: https://www.mathworks.com/help/matlab/cell-arrays.html
n = 100;
C = cell(1,n);
for k = 1:n
.. run your simulation here
C{k} = the results of your simulation
end
2 个评论
Stephen23
2022-6-4
"The issue is I have to stick with the structure. I cannot change the format."
Every cell of C contains your structure. The structure "format" does not change.
Note that you can also create a single structure array (assuming compatible sizes and fields):
S = [C{:}]
更多回答(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!