Reading in and Evaluating strings from standalone GUI using appdata

1 次查看(过去 30 天)
I have been attempting to write a standalone windows GUI for a program that I have. It necessitates that I allow the user to input a string function for a number of items and at some point in the code it executes said strings. The final value of the strings are a function of an array (T) and output an array [item name](j). There is already a string value stored in appdata with the same name (a default in case no user changes are made).
For example.
Default written into program:
setappdata(0,'Item1' , '50.^(-7590/T(j)-7.46)');
In user window, a box with a custom entry such as:
50.^(-150/T(j)+1) and a corresponding GUI code for
setappdata(0,'Item1',get(handles.edit107,'string'));
where the tag of the text box is edit107
and a subsequent evaluation of
Item1result(j)=eval(getappdata(0,'Item1'));
Assuming the T and j are both defined properly as arrays.
In the certain situation where a user might enter just a single number (which is realistic).
Is there anyway around the:
Undefined function 'eval' for input arguments of type 'double'.

回答(2 个)

Robert Cumming
Robert Cumming 2012-10-30
Am I correct in assuming the user enters any string which then gets executed in eval? This is NOT advisable.... for example: the user could enter: 'system ( 'delete C:\*' )'
Anyway - to answer your questions: have you seen
try
do something
catch % if it errors - the error is captured
do something else
end
  1 个评论
Matthew
Matthew 2012-10-30
Wow I hadn't considered that to be a possibility, yikes, thanks for the heads up. While I'm fairly confident that would never be the case, is there a safer evaluation method?
I also hadn't previously been aware of the try function, thank you!

请先登录,再进行评论。


Matthew
Matthew 2012-10-30
On this same note, is there any simpler way to check if the input is a number and if it isn't assume that it is a string and evaluate?
Ex:
if isnumeric(getappdata(0,'Item1'))
Item1R=getappdata(0,'Item1');
else
Item1R=eval(getappdata(0,'Item1'));
end
If there is no way for the eval function to process a number by itself (or an alternative function) then i need to do the above for about 150 some items...
  1 个评论
Robert Cumming
Robert Cumming 2012-11-1
You should add this to your original question - since it builds on your question - but yes isnumeric is a satisfactory way of checking if a variable is a number.
Perhaps if you expalin your aim in more detail you might get more help on avoiding eval.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by