Reading the data from another m file in a loop

조회 수: 2 (최근 30일)
Bartosz Bagrowski
Bartosz Bagrowski 2022년 5월 6일
댓글: Bartosz Bagrowski 2022년 5월 9일
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
dpb
dpb 2022년 5월 6일
Use arrays and/or named fields instead -- don't create a sequence of variable names like this to begin with.

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

채택된 답변

Harsha Kalavakolu
Harsha Kalavakolu 2022년 5월 9일
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!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by