如何将变量值放入到MATLAB中的文本字符串中?

时间:2022-03-25 07:12:05

I'm trying to write a simple function that takes two inputs, x and 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.

我试着写一个简单的函数,它接受两个输入,x和y,然后把它们传递给另外三个简单的函数,它们相加,相乘,然后除以它们。主函数应该将结果显示为包含x、y和总数的字符串。

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 f into a string? I tried different matrices and stuff like:

如何得到x, y, d, e和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?
  • 为什么函数返回"ans 3"即使我没有要求z的长度?
  • If anyone could recommend a good book for beginners to MATLAB scripting I'd really appreciate it.
  • 如果有人能推荐一本适合初学者的好书,我将非常感激。

5 个解决方案

#1


4  

As Peter and Amro illustrate, 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.

正如Peter和Amro所演示的,您必须首先将数字值转换为格式化的字符串,以显示它们或将它们与其他字符串连接起来。您可以使用函数FPRINTF、SPRINTF、NUM2STR和INT2STR来实现这一点。


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

关于将ans = 3作为输出,可能是因为您没有将输出分配给变量。如果你想获得所有的输出值,你需要用以下方法调用答案:

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

This will place the value d in out1, the value e in out2, and the value f in 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文档的价值。我自己知道的大部分东西都是我自己用的。您可以在线访问它,或者在您的MATLAB副本中使用函数DOC、HELP或HELPWIN。

#2


13  

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.

#3


6  

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:

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

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);

#4


2  

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 character array.

我刚刚意识到为什么我有这么多的麻烦——在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

#5


0  

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]

变量=['你好,这是x' x',这是y' y',最后这是d ' d '

basically

基本上

variable = [str1 str2 str3 str4 str5 str6]

变量= [str1 str2 str3 str5 str6]

#1


4  

As Peter and Amro illustrate, 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.

正如Peter和Amro所演示的,您必须首先将数字值转换为格式化的字符串,以显示它们或将它们与其他字符串连接起来。您可以使用函数FPRINTF、SPRINTF、NUM2STR和INT2STR来实现这一点。


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

关于将ans = 3作为输出,可能是因为您没有将输出分配给变量。如果你想获得所有的输出值,你需要用以下方法调用答案:

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

This will place the value d in out1, the value e in out2, and the value f in 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文档的价值。我自己知道的大部分东西都是我自己用的。您可以在线访问它,或者在您的MATLAB副本中使用函数DOC、HELP或HELPWIN。

#2


13  

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.

#3


6  

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:

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

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);

#4


2  

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 character array.

我刚刚意识到为什么我有这么多的麻烦——在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

#5


0  

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]

变量=['你好,这是x' x',这是y' y',最后这是d ' d '

basically

基本上

variable = [str1 str2 str3 str4 str5 str6]

变量= [str1 str2 str3 str5 str6]