matlab text 字符串,如何將變量值放入到MATLAB中的文本字符串中?

这篇博客探讨了如何在MATLAB中将变量值整合到文本字符串中。作者遇到问题,尝试将函数的输出(x, y, d, e, f)与字符串结合,但未能成功。解决方案包括使用FPRINTF、SPRINTF、NUM2STR或INT2STR等函数将数值转换为字符串,并使用单元数组来存储不同长度的字符串。此外,还讨论了函数返回默认值""ans 3""的原因以及如何正确获取所有输出值。" 134613219,11254346,全国天气可视化分析系统基于Hadoop实现,"['数据分析', 'hadoop', '数据库管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值