string 如何将变量值放入 M​​ATLAB 中的文本字符串中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204579/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:38:02  来源:igfitidea点击:

How do I put variable values into a text string in MATLAB?

stringmatlabtextvariables

提问by jefflovejapan

I'm trying to write a simple function that takes two inputs, xand y, and passes these to three other simple functions that add, multiply, and divide them. The main function should then display the results as a string containing x, y, and the totals.

我正在尝试编写一个简单的函数,它接受两个输入xy,并将它们传递给其他三个简单的函数,这些函数对它们进行加法、乘法和除法。然后主函数应该将结果显示为包含字符串xy和总计。

I think there's something I'm not understanding about output arguments. Anyway, here's my (pitiful) code:

我认为我对输出参数有些不理解。无论如何,这是我的(可怜的)代码:

function a=addxy(x,y)
a=x+y;

function b=mxy(x,y)
b=x*y;

function c=dxy(x,y)
c=x/y;

The main function is:

主要功能是:

function [d e f]=answer(x,y)
d=addxy(x,y);
e=mxy(x,y);
f=dxy(x,y);
z=[d e f]

How do I get the values for x, y, d, e, and finto a string? I tried different matrices and stuff like:

我如何获得的值xyde,和f成一个字符串?我尝试了不同的矩阵和类似的东西:

['the sum of' x 'and' y 'is' d]

but none of the variables are showing up.

但没有一个变量出现。

Two additional issues:

补充两个问题:

  • Why is the function returning "ans 3" even though I didn't ask for the length of z?
  • If anyone could recommend a good book for beginners to MATLAB scripting I'd really appreciate it.
  • 为什么即使我没有要求长度,函数也会返回“ans 3” z
  • 如果有人能推荐一本适合 MATLAB 脚本初学者的好书,我将不胜感激。

采纳答案by gnovice

As Peterand Amroillustrate, you have to convert numeric values to formatted strings first in order to display them or concatenate them with other character strings. You can do this using the functions FPRINTF, SPRINTF, NUM2STR, and INT2STR.

正如PeterAmro所示,您必须首先将数值转换为格式化字符串,以便显示它们或将它们与其他字符串连接起来。您可以使用函数FPRINTFSPRINTFNUM2STRINT2STR执行此操作。



With respect to getting ans = 3as an output, it is probably because you are not assigning the output from answerto a variable. If you want to get all of the output values, you will have to call answerin the following way:

关于ans = 3作为输出获取,可能是因为您没有将输出分配给answer变量。如果要获取所有输出值,则必须answer按以下方式调用:

[out1,out2,out3] = answer(1,2);

This will place the value din out1, the value ein out2, and the value fin out3. When you do the following:

这会将值d放入 中out1,将值e放入 中out2,并将值f放入 中out3。当您执行以下操作时:

answer(1,2)

MATLAB will automatically assign the first output d(which has the value 3 in this case) to the default workspace variable ans.

MATLAB 将自动将第一个输出d(在本例中为 3)分配给默认工作区变量ans



With respect to suggesting a good resource for learning MATLAB, you shouldn't underestimate the value of the MATLAB documentation. I've learned most of what I know on my own using it. You can access it online, or within your copy of MATLAB using the functions DOC, HELP, or HELPWIN.

关于建议学习 MATLAB 的良好资源,您不应低估 MATLAB 文档的价值。我已经使用它自己学到了大部分知识。您可以在线访问它,也可以使用函数DOCHELPHELPWIN在您的 MATLAB 副本中访问它。

回答by Peter

Here's how you convert numbers to strings, and join strings to other things (it's weird):

以下是将数字转换为字符串并将字符串连接到其他事物的方法(这很奇怪):

>> ['the number is ' num2str(15) '.']
ans =
the number is 15.

回答by Amro

You can use fprintf/sprintf with familiar C syntax. Maybe something like:

您可以使用熟悉的 C 语法使用 fprintf/sprintf。也许是这样的:

fprintf('x = %d, y = %d \n x+y=%d \n x*y=%d \n x/y=%f\n', x,y,d,e,f)


reading your comment, this is how you use your functions from the main program:

阅读您的评论,这就是您在主程序中使用函数的方式:

x = 2;
y = 2;
[d e f] = answer(x,y);
fprintf('%d + %d = %d\n', x,y,d)
fprintf('%d * %d = %d\n', x,y,e)
fprintf('%d / %d = %f\n', x,y,f)

Also for the answer()function, you can assign the output values to a vector instead of three distinct variables:

同样对于answer()函数,您可以将输出值分配给向量而不是三个不同的变量:

function result=answer(x,y)
result(1)=addxy(x,y);
result(2)=mxy(x,y);
result(3)=dxy(x,y);

and call it simply as:

并将其简单地称为:

out = answer(x,y);

回答by jefflovejapan

I just realized why I was having so much trouble - in MATLAB you can't store strings of different lengths as an array using square brackets. Using square brackets concatenates strings of varying lengths into a single characterarray.

我刚刚意识到为什么我遇到这么多麻烦 - 在 MATLAB 中,您不能使用方括号将不同长度的字符串存储为数组。使用方括号将不同长度的字符串连接成单个字符数组。

    >> a=['matlab','is','fun']

a =

matlabisfun

>> size(a)

ans =

     1    11

In a character array, each character in a string counts as one element, which explains why the size of a is 1X11.

在字符数组中,字符串中的每个字符都算作一个元素,这就解释了为什么 a 的大小是 1X11。

To store strings of varying lengths as elements of an array, you need to use curly braces to save as a cell array. In cell arrays, each string is treated as a separate element, regardless of length.

要将不同长度的字符串存储为数组元素,您需要使用花括号保存为元胞数组。在元胞数组中,无论长度如何,每个字符串都被视为一个单独的元素。

>> a={'matlab','is','fun'}

a = 

    'matlab'    'is'    'fun'

>> size(a)

ans =

     1     3

回答by onzyone

I was looking for something along what you wanted, but wanted to put it back into a variable.

我一直在寻找你想要的东西,但想把它放回一个变量中。

So this is what I did

所以这就是我所做的

variable = ['hello this is x' x ', this is now y' y ', finally this is d:' d]

variable = ['hello this is x' x ', this is now y' y ', finally this is d:' d]

basically

基本上

variable = [str1 str2 str3 str4 str5 str6]

variable = [str1 str2 str3 str4 str5 str6]