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

1 次查看(过去 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 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by