How the remove single apostrophe from a string?

21 ビュー (過去 30 日間)
Cheah Yue Yeou
Cheah Yue Yeou 2017 年 12 月 10 日
編集済み: Stephen23 2017 年 12 月 11 日
yfunc=input('Enter any curve equation>') %If input yfunc= 'x^2', how to remove the apostrophe so that I can use the equation...

採用された回答

Stephen23
Stephen23 2017 年 12 月 10 日
編集済み: Stephen23 2017 年 12 月 11 日
"how to remove the apostrophe so that I can use the equation..."
You can't, because a char array does not really have single quotes at the ends (unless you put them there), in just the same way the a numeric array does not contain any square brackets at the ends: the single quotes / square brackets are just the syntax that specifies how char arrays / numeric arrays are defined, but are not actually contained within the array itself. Let me demonstrate:
>> char(39) % single quote ASCII code
ans = '
>> double('hello') % get the ASCII codes for |hello|
ans =
104 101 108 108 111
Can you see the code value 39 anywhere in that list?
In any case, if you have a char array then there is nothing that can be removed that will magically turn that char array into a function. A char array is just a collection of characters, which have no meaning other than any semantic meaning from how they were defined. If those characters happen to form a function in some variable then you will need to convert it into a function using str2func, I have shown you how below:
>> str = input('Enter function of x: ','s');
Enter function of x: x.^2+x
>> fun = str2func(sprintf('@(x)%s',str));
>> fun(0:10)
ans =
0 2 6 12 20 30 42 56 72 90 110
Note that this will work as long as the function is entered using MATLAB syntax. If you want to define your own syntax then you will have to write a language parser using MATLAB, which is not a trivial task.
You really need to revise basic MATLAB types, such as what are char arrays and functions, because otherwise using MATLAB will not make sense:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by