matlab 在主 m 文件中读取输入 m 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/185461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading input m-file in a main m-file
提问by KennyMorton
Hallo, I have a question about MATLAB I am not experienced in Matlab and I would like to tell me if i have an input file (m-file) that contains some variables with their numbers ie a=5,b=6,c=7 and i want to use that m file in another program(main m-file) that uses these variables to calculate S=a+b+c. How in the main file i can read the input file?What commands should I use?What the first line should be? Assume the input file is called INP and the main MAIN. Thank you!
你好,我有一个关于 MATLAB 的问题我在 Matlab 中没有经验,我想告诉我是否有一个输入文件(m 文件),其中包含一些带有数字的变量,即 a=5,b=6,c= 7 并且我想在另一个程序(主 m 文件)中使用该 m 文件,该程序使用这些变量来计算 S=a+b+c。我如何在主文件中读取输入文件?我应该使用什么命令?第一行应该是什么?假设输入文件名为 INP 和主 MAIN。谢谢!
回答by KennyMorton
This is typically not good practice in MATLAB. The file containing the input variables would, in your example, be a script. As would your main file. MATLAB does not error when running one script from another, as suggested by ScottieT812, but under certain circumstances strange errors can arise. (Run time compiling has difficulty, variable name collisions across scripts)
这在 MATLAB 中通常不是好的做法。在您的示例中,包含输入变量的文件将是一个脚本。和你的主文件一样。正如 ScottieT812 所建议的那样,当从另一个脚本运行一个脚本时,MATLAB 不会出错,但在某些情况下可能会出现奇怪的错误。(运行时编译有困难,跨脚本的变量名冲突)
A better option is to turn the inputs script into a function which returns the variables of interest
更好的选择是将输入脚本转换为返回感兴趣变量的函数
function [a,b c] = inputs
a = 5;
b = 6;
c = 7;
Then this function can be called in the main.m script.
那么这个函数就可以在 main.m 脚本中调用了。
% main.m
[a,b,c] = inputs;
s = a+b+c;
回答by Jason S
For this sort of stuff (parameters that are easily adjusted later) I almost always use structures:
对于这类东西(稍后很容易调整的参数),我几乎总是使用结构:
function S = zark
S.wheels = 24;
S.mpg = 13.2;
S.name = 'magic bus';
S.transfer_fcn = @(x) x+7;
S.K = [1 2; -2 1];
Then you can return lots of data without having to do stuff like [a,b,c,d,e,f]=some_function;
然后你可以返回大量数据而不必做像 [a,b,c,d,e,f]=some_function;
One nice thing about structures is you can address them dynamically:
关于结构的一个好处是你可以动态地处理它们:
>> f = 'wheels';
>> S.(f)
ans =
24
回答by Mr Fooz
It sounds like you want to have some global configuration information that's used by scripts. Often, it's much better to create functions and pass values as arguments, but sometimes it makes sense to do things the way you suggest. One way to accomplish this is to save the information in a file. See "load" and "save" in the Matlab documentation.
听起来您想要一些脚本使用的全局配置信息。通常,创建函数并将值作为参数传递会更好,但有时按照您建议的方式做事是有意义的。实现此目的的一种方法是将信息保存在文件中。请参阅 Matlab 文档中的“加载”和“保存”。
回答by Scottie T
If your "input" file is an m-file, just use the name of the file in your "main" m-file. For example you might have a file called input.m that looks like this:
如果您的“输入”文件是一个 m 文件,只需使用“主”m 文件中的文件名。例如,您可能有一个名为 input.m 的文件,如下所示:
% File: inputs.m
a = 5;
b = 6;
c = 7;
Then, you can use it in the file main.m like this:
然后,您可以像这样在文件 main.m 中使用它:
% File: main.m
inputs;
S = a + b + c;
回答by b3.
I ran into the exact problem KennyMortonmentioned when trying to create runtime compiled versions of MATLAB software for my work. The software uses m-files extensively for passing arguments between functions. Additionally, we create these m-files dynamically which the deployed version of MATLAB does not play nice with. Our workaround was:
我在尝试为我的工作创建运行时编译版本的 MATLAB 软件时遇到了KennyMorton提到的确切问题。该软件广泛使用 m 文件在函数之间传递参数。此外,我们动态创建了这些 m 文件,部署的 MATLAB 版本不能很好地处理这些文件。我们的解决方法是:
- save the parameters to a file without the .m extension
- read and eval the contents of the file
- 将参数保存到没有 .m 扩展名的文件中
- 读取并评估文件的内容
So, to follow the OPs example, in a function we would create a text file, INP, containing our parameters. We create this file in the directory returned by the ctfrootfunction. Then, in MAIN, we would use the following to retrieve these parameters:
因此,按照 OP 示例,在函数中,我们将创建一个文本文件 INP,其中包含我们的参数。我们在ctfroot函数返回的目录中创建这个文件。然后,在 MAIN 中,我们将使用以下内容来检索这些参数:
eval(char(textread(fullfile(ctfroot, INP), '%s', 'whitespace', '');
回答by Todd
If the data script is just a script, you can call it from a function or another script directly. Not extra commands required. For example:
如果数据脚本只是一个脚本,则可以直接从函数或其他脚本中调用它。不需要额外的命令。例如:
%mydata.m
a = 1;
b = 2;
%mymain.m
mydata
whos
mymain
>>mymain
Name Size Bytes Class Attributes
>>mymain
名称大小字节类属性
a 1x1 8 double
b 1x1 8 double
a 1x1 8 双
b 1x1 8 双
This also works for functions in addition to scripts
这也适用于脚本以外的函数
%foo.m
function foo
mydata
whos>>foo
%foo.m
函数 foo mydata
whos >>foo
Name Size Bytes Class Attributes
名称大小字节类属性
a 1x1 8 double
b 1x1 8 double
a 1x1 8 双
b 1x1 8 双
Generally, it is preferable to use a MAT or other data file for this sort of thing.
通常,最好使用 MAT 或其他数据文件来处理此类事情。

