string 我正在尝试在 matlab 中显示变量名称和它们值的 num2str 表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13299923/
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
I am trying to display variable names and num2str representations of their values in matlab
提问by cuabanana
I am trying to produce the following:The new values of x and y are -4 and 7, respectively, using the disp and num2str commands. I tried to do this disp('The new values of x and y are num2str(x) and num2str(y) respectively'), but it gave num2str instead of the appropriate values. What should I do?
我正在尝试生成以下内容:使用 disp 和 num2str 命令,x 和 y 的新值分别为 -4 和 7。我试图这样做 disp('x 和 y 的新值分别是 num2str(x) 和 num2str(y)'),但它给出了 num2str 而不是适当的值。我该怎么办?
回答by Eitan T
Like Colin mentioned, one option would be converting the numbers to strings using num2str
, concatenating all strings manually and feeding the final result into disp
. Unfortunately, it can get veryawkward and tedious, especially when you have a lot of numbers to print.
就像 Colin 提到的那样,一种选择是使用 将数字转换为字符串num2str
,手动连接所有字符串并将最终结果输入disp
. 不幸的是,它会变得非常笨拙和乏味,尤其是当您要打印大量数字时。
Instead, you can harness the power of sprintf
, which is very similar in MATLAB to its C programming language counterpart. This produces shorter, more elegant statements, for instance:
相反,您可以利用 的强大功能sprintf
,这在 MATLAB 中与其 C 编程语言非常相似。这会产生更短、更优雅的语句,例如:
disp(sprintf('The new values of x and y are %d and %d respectively', x, y))
You can control how variables are displayed using the format specifiers. For instance, if x
is not necessarily an integer, you can use %.4f
, for example, instead of %d
.
您可以使用格式说明符控制变量的显示方式。例如,如果x
不一定是整数,则可以使用%.4f
,例如,代替%d
。
EDIT: like Jonas pointed out, you can also use fprintf(...)
instead of disp(sprintf(...))
.
编辑:就像乔纳斯指出的那样,您也可以使用fprintf(...)
代替disp(sprintf(...))
.
回答by Colin T Bowers
Try:
尝试:
disp(['The new values of x and y are ', num2str(x), ' and ', num2str(y), ', respectively']);
You can actually omit the commas too, but IMHO they make the code more readable.
您实际上也可以省略逗号,但恕我直言,它们使代码更具可读性。
By the way, what I've done here is concatenated 5 strings together to form one string, and then fed that single string into the disp
function. Notice that I essentially concatenated the string using the same syntax as you might use with numerical matrices, ie [x, y, z]
. The reason I can do this is that matlab stores character strings internally AS numeric row vectors, with each character denoting an element. Thus the above operation is essentially concatenating 5 numeric row vectors horizontally!
顺便说一下,我在这里所做的是将 5 个字符串连接在一起形成一个字符串,然后将该单个字符串输入到disp
函数中。请注意,我基本上使用与数字矩阵相同的语法连接字符串,即[x, y, z]
. 我可以这样做的原因是 matlab 在内部将字符串存储为数字行向量,每个字符表示一个元素。因此,上述操作本质上是水平连接 5 个数字行向量!
One further point: Your code failed because matlab treated your num2str(x) as a string and not as a function. After all, you might legitimately want to print "num2str(x)", rather than evaluate this using a function call. In my code, the first, third and fifth strings are defined as strings, while the second and fourth are functions which evaluate to strings.
还有一点:您的代码失败,因为 matlab 将您的 num2str(x) 视为字符串而不是函数。毕竟,您可能合情合理地想要打印“num2str(x)”,而不是使用函数调用来评估它。在我的代码中,第一个、第三个和第五个字符串被定义为字符串,而第二个和第四个是计算为字符串的函数。