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

1 view (last 30 days)
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?

Accepted Answer

Image Analyst
Image Analyst on 9 Apr 2012
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 Comments
james
james on 9 Apr 2012
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 on 9 Apr 2012
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().

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!