I have an .m file with store variables, how do I call it in a script to get the stored variables?

조회 수: 2 (최근 30일)
I have an .m file that looks like this:
%Information
%Information
%Information
C = [1 1 0 0 0;
1 0 1 0 1;
0 1 1 1 0;
0 0 0 1 1];
Sx = [1 0 0;
0 0 0;
0 0 0;
0 0 0];
Sy = [0 1 0;
0 0 1;
0 0 0;
0 0 0]
X = [0 1 .5 1 .5];
Y = [0 0 1 1 .3];
L = [0 0 0 0 0 0 1 0];
I am trying to save each line of "info" as a separate string per line to be displayed later, and then call the variables, C, Sx, Sy, X, Y, L in a script. How do I do this?

채택된 답변

Image Analyst
Image Analyst 2012년 4월 9일
For example in your other m-file script:
C_copy = C;
Sx_copy = Sx;
plot(X, Y);
regionprops(L);
and so on. Note, that works because the first script leaves those variables in the base workspace. If it were a function, they'd be gone once the function gets done running.
  댓글 수: 2
james
james 2012년 4월 9일
Thanks (new to matlab).
2 questions: If wanted to get the information in the notes of the script to be displayed later, how would I get that?
Also, if I wanted to ask the user to input the name of the script with the above information. for example,
filename=input('Please enter the file name:'), how would I go about calling the filename and use it above. So, the user calls the file name with the information above, and I'm able to use the variables in that file. This is so I don't have to change the script each time I want to call another file with a different set of inputs.
Image Analyst
Image Analyst 2012년 4월 9일
You can use uigetfile() to have the user select the file. Don't make them know the name of the file in advance and have to type it in - that's cruel.
You can get comments (I assume that's what you mean by "notes") by opening the file with fopen(), reading line by line with fgetl() and using strfind() to look for percent symbols that are not enclosed within a string (look for single quotes), and take the rest of the line after the percent symbol. You may also want to take a look at lookfor().

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by