Matlab GUI edit text 'pi' and mathematical operations

If a person decides to enter 'pi' into the edit box, is there a way to convert the string 'pi' into its numerical value? So right now the callback function looks like:
a = str2double(get(handles.editbox,'string'));
When a value is entered, e.g. '2', the function str2double converts the string '2' into its numerical value. Is it possible to achieve something like this with 'pi' or other matlab built-in constants?
Also, is there a way to input mathematical operations into the edit boxes? Say I input 'pi/2' into the text box. Is there a way to 'compute' this string and convert it to its numerical value?

 Risposta accettata

str2num() will do that calculation.
str2num() works by using eval() on whatever string is passed in. So if the user put
delete('*')
into the field, and you used str2num() on it, then all of your files in the current directory wold get deleted.

2 Commenti

Thanks, so that settles one problem. Now I have another one that I haven't thought through... If the user decides to input something that isn't a number, my matlab code will generate an error, and I want to avoid that. And I certainly don't want them to input delete('*')...
How can I fix that? I certainly need to be able to input numbers, mathematical operations, pi, etc. But anything else with cause my code to fail. I thought of using something like this:
check = [a b c d e] %contains values
if sum(isnan(check))~=0
uiwait(errordlg('error','error','modal'))
return
end
This code doesn't work. What exactly does str2num return when it's called on a string?
e.g.:
str2num('string')
In matlab, I get
[]
a 0 by 0 matrix?
EDIT: I managed to fix it by doing the following:
if size(check,2)~=5
uiwait(errordlg('error','error','modal'))
return
end
I would still like to hear your answer if you have a more efficient solution.
The documentation says,
If the input argument does not represent a valid number or matrix, str2num(str) returns the empty matrix in x.
That turns out not to be the case in R2016b, and I have just filed a bug report about it.
In R2016b, string happens to be the constructor for a datatype that is new as of R2016b, string('hello') constructing "hello" so string by itself would be string() which would construct "" the empty string. And then R2016b does not do proper error checking in str2num() so it turns out that in R2016b,
str2num('string')
would return "" the empty string (in the new-type sense)
But it should return [] . Unless you had happened to use
string = some value
in which case it should return the value if it was numeric...
In many programming languages, you can get part-way there on input validation by rejecting strings that contain '(' because in those languages, there are no possible function calls except with () syntax. That is not the case for MATLAB, though: a plain string can be a function name by itself, possibly called with no arguments. And then there is command/function equivalence, so
str2num('delete *')
would become equivalent to delete('*')
The much more robust method is to strictly constrain the allowed tokens and relationship between tokens, and parse it, never calling user-provided names, only names that you already knew, passing in parameters that you had proven are safe, and making sure that the names you call track back to the built-in versions.
Remember, the user might have done
function pi(varargin)
delete('*')
end
so if you look at 'pi' and say "I know that, it is safe!" and eval the string, then you would be invoking the user-provided routine unless you had protected against that possibility, such as by using builtin('pi')

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by