Can I execute MATLAB script that is written in Edit box in a GUI created by GUIDE?
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 28 de Oct. de 2015
And when the user enters the text
!delete *.*
?
13 comentarios
Mohamed Abdelkader Zahana
el 28 de Oct. de 2015
Walter Roberson
el 28 de Oct. de 2015
If you can limit the execution to a subset of commands, then you can parse the command line to extract parameters that you pass to your own routines instead of asking MATLAB to execute the code.
"Scrubbing" input before executing it is a persistent security problem. For example a bunch of web sites that thought they were securing strings just fine turned out to fail badly when UTF-8 encoding created new ways of inputting close-quotes and so allowed people to inject arbitrary commands onto sites. See also the history of "SQL Injection" hacks.
Steven Lord
el 28 de Oct. de 2015
And when you qualify your inputs incorrectly, incompletely, or not at all you can end up spending time with Bobby Tables.
Tareq Rahmani
el 22 de Mzo. de 2020
Hi all
How can I save a whole MATLAB script in a string , then run that string without saving the script to any file ?
Walter Roberson
el 22 de Mzo. de 2020
>> s = sprintf('disp("this is line 1");\ndisp("this is line 2")')
s =
'disp("this is line 1");
disp("this is line 2")'
>> eval(s)
this is line 1
this is line 2
We do not recommend using eval() however; there are many ways for it to go wrong. Such as if the user enters
!delete *.*
Tareq Rahmani
el 23 de Mzo. de 2020
Thanks very much .
Now, I have this problem. The below is a function which is called on button pressed.
function ButtonPushed(app, event)
x = app.TextArea.Value;
str = regexprep(x,'\n',';');
msgbox(str );
%eval(str);
% msgbox( sprintf('%f',a));
end
In fact, I am reading from a textarea many lines like below :
x = [0:1]
y=sin(x)+cos(x)
plot(x,y)
and I want to make them one line (separated by ; ) like below :
x = [0:1];y=sin(x)+cos(x);plot(x,y)
I spent all the night and didn't get how ? Thanks for you.
Walter Roberson
el 23 de Mzo. de 2020
The Value property from a text area does not have any \n characters. No matter how you initialize it, what is returned when you retrieve the Value is a cell array of character vectors.
For example,
function ButtonPushed(app, event)
x = app.TextArea.Value;
str = strjoin(strtrim(x), ';');
msgbox(str );
%eval(str);
% msgbox( sprintf('%f',a));
end
Tareq Rahmani
el 23 de Mzo. de 2020
Thanks very much
Excellent !
Tareq Rahmani
el 23 de Mzo. de 2020
Can I have your support .
what is wrong with the below : it shows me error :
function TeFlowButtonPushed(app, event)
x = app.TextArea.Value;
str = strjoin(strtrim(x), ';');
% msgbox(str );
%eval(str);
% msgbox( sprintf('%f',a));
x=strsplit(str,';');
for k = 1:length(x)
YourMatrix = [eval(x(k))];
table_as_cell = num2cell(YourMatrix);
table_handle = app.UITable;
set(table_handle, 'Data', table_as_cell);
end
end
it shows error: Error using eval Must be a string scalar or character vector
Tareq Rahmani
el 23 de Mzo. de 2020
what is wrong here

Walter Roberson
el 23 de Mzo. de 2020
The text you are eval includes an = assignment. You cannot assign the output of such statements: it would be like having the line
Matx = x = 7
You can only assign the output of eval when it computes a value without assignment.
See also evalc()
Tareq Rahmani
el 24 de Mzo. de 2020
Editada: Walter Roberson
el 24 de Mzo. de 2020
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
el 24 de Mzo. de 2020
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')
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!