Use names defined in array as input variables in for loop

22 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 15 Dic 2022
"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?

Accedi per commentare.

Risposte (2)

Jan
Jan il 15 Dic 2022
Spostato: Jan il 15 Dic 2022
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 Commenti
Image Analyst
Image Analyst il 16 Dic 2022
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.
Raymon
Raymon il 16 Dic 2022
Thank you for your reactions. I now get that using variable names like I did here is not right and I will fix it.
However, I think this is not the core of my problem in my question. The core is that I want to know how to do the same transformation in a loop for TimeTables with different names (Temperature, Pressure, Humidity).
So to make the question simpler and more clear: I have 3 TimeTables, called Temperature, Pressure, Humidity. I want to multiply each data value in this time table by 5 (just for the example).
Normally I would write this like
TemperatureMultiplied = Temperature.Data.*5
PressureMultiplied = Pressure.Data.*5
TemperatureMultiplied = Temperature.Data.*5
However, I have 50 output variables (like Temperature, Pressure, Humidity), and per variable I have to do like 10 transformations (like the multiplication with 5 for this example), so therefore it is not feasible to do it in this way.
So therefore I am looking for a way to do those transformations in a for-loop. Where as an input for the for loop is an array which includes the names of the timetables I want to do those thransformations on.
I hope I made myself more clear now.

Accedi per commentare.


Sara Nadeau
Sara Nadeau il 15 Dic 2022
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.
  1 Commento
Raymon
Raymon il 16 Dic 2022
I use ToWorkspace blocks to write the data in TimeSeries to workspace. It is a SimulationOutput object, and in this object I can find all TimeSeries.

Accedi per commentare.

Categorie

Scopri di più su Time Series Collections in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by