Running my script multiple times and storing variables in a matrix

2 次查看(过去 30 天)
Hi,
I have a script (attached file) that produces five variables from the data of a chosen week (the user can chose a week in a user interaction box, the chosen number of the week is used to extract the needed data from the corresponding column of the table PVUebersicht) and a chosen percentage of one particular parameter.
Now I'd like to run the script 52 times, so I'd like to get the 5 variables for every week of the year and store it in a matrix. For all of those weeks the percentage would stay the same.
How is this possible? I need to somehow have a for loop in an external script, but how can I specify that each week has to be chosen and the percentage without changing the original script?
Thank you for your help!
  1 个评论
Noush
Noush 2022-1-14
When I use this for loop in a seperate script and just remove the dialog boxes, it runs 52 times through all the weeks, but it only saves the variables for week 52.
for Zahl_Woche=1:52
Jahresuebersicht
Jahr = [Eigenverbrauchsanteil Autarkiegrad AutarkiegradMobil AutarkiegradGrundlast AutarkiegradEinspeisung];
end

请先登录,再进行评论。

采纳的回答

Max Heimann
Max Heimann 2022-1-14
编辑:Max Heimann 2022-1-14
I am not sure i understand what you are trying to achieve. What percentage are we talking about? It sounds like you want to call the function 52 times and store all results. In that case you need to define a different place to store your returned data for each loop cycle.
There are several ways to achieve this. E.g. Matlab allows array sizes to dynamically change during runtime. What you could do is initalize "Jahr" as an empty array and then append the new values each cycle.
Jahr = [];
for Zahl_Woche=1:52
Jahresuebersicht
Jahr = [Jahr; Eigenverbrauchsanteil Autarkiegrad AutarkiegradMobil AutarkiegradGrundlast AutarkiegradEinspeisung];
end
  3 个评论
Noush
Noush 2022-1-14
Is there a way to do the same thing but as a table? I tried this and it again only calculates the last week:
sz = [52 6]; %Dimensionen der Jahresübersichttabelle
varArten = {'double','double','double','double','double','double'};%Arten der Variablen
varNamen = {'KGesamt','Eigenverbrauchsanteil','Autarkiegrad','AutarkiegradMobil','AutarkiegradGrundlast','AutarkiegradEinspeisung'};%Überschriften der Spalten in der Tabelle:
Jahr = table('Size',sz,'VariableTypes',varArten,'VariableNames',varNamen);
for Zahl_Woche=1:52
Jahresuebersicht
Jahr = [KGesamt Eigenverbrauchsanteil Autarkiegrad AutarkiegradMobil AutarkiegradGrundlast AutarkiegradEinspeisung];
end
Noush
Noush 2022-1-14
And also, my plot overlaps. Is there a way to define that the plots for the weeks are supposed to come after one another?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by