For a function, how do I make the function take strings as input?

function [ ] = square( a ,b )
a is supposed to be a string value; and b is a number.
I've tried manipulating the value after this line by using num2str(a) & a couple of other methods, but nothing I've done works. I always get this error "Undefined function or variable 'whatever'."
Please help, thank you.

2 件のコメント

Preethi
Preethi 2016 年 11 月 14 日
can you elaborate?
Manjeel Regmi
Manjeel Regmi 2016 年 11 月 14 日
How do I elaborate? That's everything I want - a function that takes in string values as input. My error is "Undefined function or variable 'whatever string I input'. Sorry if that wasn't clear.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 14 日

1 投票

a_value = num2str(a);
or, better,
a_value = str2double(a);
Note that the routine has to be run by a line of code, or by going to the command line and typing in the command, such as
square('341', 17.2)

1 件のコメント

Walter Roberson
Walter Roberson 2016 年 11 月 14 日
In MATLAB, it is not possible to define a function that knows to automatically change a parameter to a string. You cannot, for example, define square such that
square(EFG, 17.2)
should be automatically interpreted as
square('EFG', 17.2)
In each case where you want to pass a string, you need to quote the string when you construct it, like the example I just showed, or like
square_arg = 'EFG';
square(square_arg, 17.2)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by