Accessing variables in m file without executing it

조회 수: 12 (최근 30일)
Pavan
Pavan 2017년 8월 10일
답변: Jan 2017년 8월 10일
I have a .m file that will only run as part of a giant program. However, I want to access some variables in it without running this program. Is this possible?
  댓글 수: 1
Stephen23
Stephen23 2017년 8월 10일
"Is this possible?"
possible: yes.
good idea: no.
Basically you will have to write your own code parser. It would be better to write your code so that those variables are stored/saved somewhere more accessible (e.g. a .mat file, a CSV file, a function that defines parameter values, etc.).

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

채택된 답변

Jan
Jan 2017년 8월 10일
I agree with Stephen, but have a slughtly more pessimistic opinion:
No, it is not possible, at least not in the general case. You could parse the file and recognize some trivial definitions like:
a = 10;
But as soon as it gets more complex, you need a sophisticated parser. Of course you can create such a parser, but this would be a duplicate of Matlab. And even then the code must be interpreted to get all values, e.g. in:
a = 10;
b = a;
It is a good programming practice, to separate the processing, the gui and the data strictly. Instead of storing a bunch of values inside the function, a MAT file is much better, most of all if the data are needed anywhere else also.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by