在 matlab 中创建输出 m 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/192796/
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
Creating output m-file in matlab
提问by b3.
Suppose I have an M-file that calculates, for example? d=a+b+c(The values on a, b, cwere given earlier).
例如,假设我有一个计算的 M 文件?d=a+b+c(a, b,上的值c是之前给出的)。
What command should I use in order to produce an output M-file showing the result of this sum?
我应该使用什么命令来生成显示该总和结果的输出 M 文件?
回答by Azim
In Matlab a semicolon ";" at the end of a line suppresses output. So,
在 Matlab 中,分号“;” 在一行的末尾抑制输出。所以,
>> d=1+2;
>> d=1+2
d =
3
Or you can use dispas in the first answer.
或者您可以像第一个答案一样使用disp。
>> disp(num2str(d));
3
If you want to write the values of a variable to a file you can use either dlmwrite(use Matlab's help function to get more info) or savecommands. For dlmwrite, the usage is basically
如果要将变量的值写入文件,可以使用dlmwrite(使用 Matlab 的帮助功能获取更多信息)或保存命令。对于dlmwrite,用法基本上是
>> dlmwrite('filename',d,',')
which writes the vector (matrix), d, to the text file named filenameusing a comma as the delimiter between elements.
它使用逗号作为元素之间的分隔符将向量(矩阵)d 写入名为filename的文本文件。
The other option is to use the savecommand, as in
另一种选择是使用save命令,如
>> save('filename','d')
which will save the variable 'd' to a MAT file (see help savefor more information). Hope this helps?
这会将变量 'd' 保存到 MAT 文件中(有关更多信息,请参阅帮助保存)。希望这可以帮助?
回答by b3.
To expand on Azim's answer, the savecommand can be used to save variables to a text file. In your case you would use:
为了扩展Azim 的回答,可以使用save命令将变量保存到文本文件。在您的情况下,您将使用:
save 'filename' d -ascii
回答by Scottie T
disp(num2str(d));

