Use names defined in array as input variables in for loop
29 次查看(过去 30 天)
显示 更早的评论
I have simulation results from Simulink. I simulate 1 year at a time. After that, I want to merge all result of the different years into 1 array for each output. I know how to do that. But, I have approximately 50 outputs, and I want to know how to do this in a smart way.
For the example, let say I have 3 outputs from the simulation:
Temperature, Humidity, and Pressure, and let say we simulate 2 years.
For temperature, this gives a timetable of Temperature1 (result of 1st year) and Temperature2 (result of second year). We also have Humidity 1 and 2 and Pressure 1 and 2.
Currently, I merge each result in the following way:
TotalTemperature = [Temperature1; Temperature2];
TotalHumidity = [Humidity1; Humidity2];
TotalPressure = [Pressure1; Pressure2];
However, this way is not feasible to do with 50 outputs, so I want to use a for loop.
I want the input for the loop to be an array of
Array = ['Temperature' 'Humidity' 'Pressure']
Or something like that, and then the output to be the same as in the lines above.
The thing I am struggeling with is how to use the names in Array in the loop. I have to do more transformations in a similar way, but I think if this question is answered I can do it is the same way.
1 个评论
Stephen23
2022-12-15
"The thing I am struggeling with is how to use the names in Array in the loop."
Yes, that would be a struggle.
But why are you forcing pseudo-indices into variable names, when you could use actual indices?
回答(2 个)
Jan
2022-12-15
移动:Jan
2022-12-15
This is one of the most frequently asked questions. See: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval .
The answer is: Don't do this. It is a shot in your knee. Do not hide indices in the names of variables as in "Pressure1", "Pressure2", but create an array directly using real indices.
Applying a complicated method to hide an index in the name demands for even more complicates methods to access these variables later on. So TotalTemperature = [Temperature1; Temperature2]; is a good point to start, but why do you create "Temperature1" at first?
2 个评论
Image Analyst
2022-12-16
I'll just add in my vote. Believe @Jan and don't do this. If you don't know the variable name in advance, when writing the code, and it only gets a name afterwards (during run time), then how will you refer to the variable with the unknown name while you're writing the code? Yes, it's possible but usually no one wants to tell you how because it's such a bad idea. There are reasons why this idea, along with using eval(), are probably the two concepts most strongly recommended against by nearly everyone. It might seem like a good idea to you, but it's really not. Trust the experts on this.
Sara Nadeau
2022-12-15
Aside from the discussion about the input to the loop, I am wondering how you're logging the data from simulation. What format is the data in when it comes out of the simulation? For example, is all the data returned as a Simulink.SimulationOutput object? Are you trying to pull data from a Simulink.SimulationData.Dataset object? Something else?
Both of these objects have several functions that could help with postprocessing data in a loop.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!