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

Walter Roberson
Walter Roberson 2022년 6월 4일
Why? Why do you not assign them to the desired name in the first place?
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022년 6월 4일
편집: Muhammad Qaisar Fahim 2022년 6월 4일
I am running some models and in the result a structure is saved every time. Sometimes I want to compare 2 solutions, sometimes 3 and some times n solutions. (Comparing solution means making alot of plots and doing alot of calculations. Instead of changing script every time for loop will help to reduce effort tremendously .
Save each struct to a different file. At an appropriate time, load() with an output variable
model1 = load(...)
model2 = load(...)
now compare model1 and model2
Stephen23
Stephen23 2022년 6월 4일
편집: Stephen23 2022년 6월 4일
Rather than forcing pseudo-indices into variable names or fieldnames, your code would be simpler and more efficient if you used actual indices.
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022년 6월 4일
so for plots say I want to plot
figure();hold on; grid on;
plot(model1.x)
plot(model1.x)
Now in this example I need to write two lines of code. how can I automate it somehoe e.g.,
for
figure();hold on; grid on;
plot()
end
I am asking this as I have to generate 100 of plots and I cannot repeat the code
Stephen23
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?
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022년 6월 4일
You are right. My explanation is not good. Let me try again. I run simulation and as a result of simulation I get results in the form of structure. Say structure1. Then I change some parameters and run my simulation again Now I again get results in the form of structure. Say structure2. Now variables names inside thses structures are same but their values will be different. Their could be n number of structures and the number of variables inside the structure are 100 or more each. Now I want to plot some variables of those n structures on top of each other to compare. I an thinking of a solution to name the structures before the loop such that I only give n (i.e., number of structures I want to compare) and my plots are generated and I dont need to write 100 lines for 100 structures. I hope the question is clear now ?

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2022년 6월 4일

1 개 추천

n = 100;
C = cell(1,n);
for k = 1:n
.. run your simulation here
C{k} = the results of your simulation
end

댓글 수: 2

Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022년 6월 4일
The issue is I have to stick with the structure. I cannot change the format.
"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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2015b

태그

질문:

2022년 6월 4일

댓글:

2022년 6월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by