Can I execute MATLAB script that is written in Edit box in a GUI created by GUIDE?

3 ビュー (過去 30 日間)
Hi,
I created a GUI using GUIDE. I would like to allow the user to input MATLAB script in an Edit box. Then I execute the string inside the Edit box as a MATLAB script. Is this possible? Thanks in advance.

採用された回答

Mohamed Abdelkader Zahana
Mohamed Abdelkader Zahana 2015 年 10 月 28 日
I think I found the answer, and have tested it successfully.
One can use:
eval(expression)
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 28 日
One cannot use that securely. And using it to make changes in the state of your program is really fragile. "poofing" variables into existence inevitably ends badly.
And are you at least using try/catch around the eval() to account for the pretty much 100% probability that someone will make a typing mistake and enter text that is not a valid command?

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 10 月 28 日
And when the user enters the text
!delete *.*
?
  13 件のコメント
Tareq Rahmani
Tareq Rahmani 2020 年 3 月 24 日
編集済み: Walter Roberson 2020 年 3 月 24 日
Hi Walter ,
I hope you are fine. Thanks for answers. I have this script :
str = '';
x = app.TextArea.Value;
str = strjoin(strtrim(x), ';');
% msgbox(str );
%eval(str);
% msgbox( sprintf('%f',a));
%x=strsplit(str,';');
%YourMatrix = [eval(str)];
%YourMatrix = [evalin('base',(str))];
%table_as_cell = num2cell([eval(str)]);
table_handle = app.UITable;
set(table_handle, 'Data', num2cell([evalc(str)]) ); % set(table_handle, 'Data', num2cell([evalc(str)]));
and it gives me this below output in table : I don't want that the words splitted into cells where each letter in one cell. I want that whole word be in one cell !
If you look the ans is splitted into a n s in different columns !
Walter Roberson
Walter Roberson 2020 年 3 月 24 日
evalc() captures as text, not as number. There is no guarantee that the string evaluated only outputs numbers. Indeed, since it does not have terminating semicolon, it outputs
x =
2
y =
3
ans =
2.23606797749979
and it is not clear what exactly you would want your Data to be set to in that case.
If you put in the terminating semi-colons on every line, and if you can be sure that they did not use an assignment statement for the last expression, such as if you ask to execute 'x=2; y=3; sqrt(x+y);' then you can eval() the string and use ans . But can you be sure that the user will not ask for
x=2
y=3
z=sqrt(x+y)
fprintf("z = %g\n", z)
??
evalc('x=2; y=3; z=sqrt(x+y); fprintf("z = %g\n", z);')
changes ans to z = 2.23607(newline) and it is not clear you would want that as your Data property.
When you permit the user to input the commands and you execute them with evalc(), the only output you can expect is a character vector. You can split the character vector by lines, such as
regexp(evalc('x=2; y=3; z=sqrt(x+y); fprintf("z = %g\n", z);'), '\n', 'split')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by