How the remove single apostrophe from a string?

9 visualizzazioni (ultimi 30 giorni)
yfunc=input('Enter any curve equation>') %If input yfunc= 'x^2', how to remove the apostrophe so that I can use the equation...

Risposta accettata

Stephen23
Stephen23 il 10 Dic 2017
Modificato: Stephen23 il 11 Dic 2017
"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:

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by