Reading the data from another m file in a loop

4 views (last 30 days)
Hi guys, I created two functions, in the second one I'd like to read the data from the first one like this but in a smarter way, is it possible to use a loop somehow here? I'd like to read from t1 till t4 and from y1 till y50 and it's not convinient to write every single variable.
t1=model.t1;
t2=model.t2;
t3=model.t3;
t4=model.t4;
y1=model.y1;
y2=model.y2;
y3=model.y3;
y4=model.y4;
y5=model.y5;
y6=model.y6;
y7=model.y7;
y8=model.y8;
y9=model.y9;
y10=model.y10;
  1 Comment
dpb
dpb on 6 May 2022
Use arrays and/or named fields instead -- don't create a sequence of variable names like this to begin with.

Sign in to comment.

Accepted Answer

Harsha Kalavakolu
Harsha Kalavakolu on 9 May 2022
As per my understanding you want to send/access data from one function file to another function file.
You can go through the documentation below to see different ways of achieving that.
However, as you want to access many different variables try to put all those variables into a cell array and pass that as function arguments.
You can send this cell array to any function as argument and can access that from that function.
e.g.:
>>my_variables={t1, t2,t3, t4.......}
t1, t2 are variables in func1 and you want to access these in func2
then create cell array in func1 with t1, t2 as members and send cell array as argument to func2 from func1.
Here is an alternate solution:
If you do not want both functions to work simultaneously then you can save all the variables from workspace into a “.m“ file using save command and can load all these variables into workspace using load command.
Hope it helps!

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by