How the remove single apostrophe from a string?
10 次查看(过去 30 天)
显示 更早的评论
yfunc=input('Enter any curve equation>') %If input yfunc= 'x^2', how to remove the apostrophe so that I can use the equation...
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!