MATLAB Answers

How to run a custom command from a UI

11 views (last 30 days)
Guy Stimpson
Guy Stimpson on 23 Dec 2019
Edited: Adam Danz on 30 Dec 2019
Hi,
I'm designing a UI for plotting various instrumental data. However, I'd like to have a text field in which the user can enter any Matlab command they want and have it executed, as if they were using the Matlab console. The command would be entered as standard Matlab syntax, and would be processed as follows:
function SomeButtonPushed(app, event)
command = app.aTextField.Value;
% Some code to execute command
end
I would also like to get the console output (or whatever equivalent) in terms of error messages etc.
Is this possible? If so, how should I go about it?
Many thanks in advance.

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 23 Dec 2019
"...in which the user can enter any Matlab command they want and have it executed..."
Does this include, for example, commands like (pseudocode):
system format my entire harddrive
quit MATLAB
delete everything in the workspace
reallocate any variable used in your code
system uninstall MS Office
"Is this possible? If so, how should I go about it?"
evalc
Guy Stimpson
Guy Stimpson on 23 Dec 2019
If those commands are executable from Matlab normally, then yes, it should include them. The 'new' command window should operate in exactly the same manner as the Matlab command window, or at least as close as possible.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 Dec 2019
Edited: Adam Danz on 30 Dec 2019
Another approach is to write the set of commands to an m-file that can run as a script or a function and then you can call that script/function from within the GUI. This method allows for evaluation of more complex code than the evalc() approach.
Here's a demo.
Part 1: Create GUI with edit text window and 'Compute' button. The text window will contain some code to be analyzed.
fh = figure();
editBoxHandle = uicontrol('style','edit','units','normalize','position',[.1 .1 .6 .6],'Max',2,...
'String',['x = -pi : 0.001 : pi;', newline, 'y = zeros(size(x));', newline, newline, ...
'for i = 1:numel(x)', newline, ' y(i) = sin(x(i));', newline, 'end'], ...
'HorizontalAlignment','Left');
uicontrol('style','PushButton','units','normalize','position',[.3 .75 .2 .1],...
'String','Compute','Callback',{@mainWindowCommandFcn, editBoxHandle});
Part 2: Define the callback function to the 'Compute' button. The 3rd input is the edit box handle. If you're using GUIDE or APP DESIGNER, you can get this handle through the handles or app structure, respectively. Before running this code, edit the path to the m-file (see mfileName variable).
function mainWindowCommandFcn(hObj,~, editBoxHandle)
% Get text from edit-text window
commandTxt = editBoxHandle.String;
% make sure each line ends with (at least one) carriage return
commandTxt = [commandTxt,repmat(newline,size(commandTxt,1),1)];
% Create/overwrite m-file (replace the path)
mfileName = fullfile('C:\Users\name\Documents\MATLAB','mainWindowCommandFcn.m');
fid = fopen(mfileName,'w+');
if fid<0
% Throw error if there was a problem creating/writing the m-file
error('Error accessing %s',mfileName)
end
fprintf(fid,'%s',sprintf('%s',commandTxt'));
fclose(fid);
% To see the new file: open(mfileName)
% make sure the m-file is on path and execute it
if exist(mfileName,'file')~=2
addpath(fileparts(mfileName))
end
run(mfileName); % This will run the scrip; now 'x' and 'y' are available
% Now you have access to the variable created within the script or
% the outputs provided by the function.
disp(y) %display the value of y
end
Caveats:
  • If the m-file you create is a script, any variable names created by the user in the edit-window will overwrite variable names in your callback function. To decrease the chances of error, use long and strange variable names in your callback function.
  • If the user does not suppress outputs using a semicolon, results for those lines will display in the command window. You can easily add semicolons to all lines in order to avoid that.
  • If the user's code is not functional, errors/warnings will be thrown from the m-file but the error stack will clearly show that the callback function called the m-file so error tracing shouldn't be too difficult.
  • What ever variable you use after evaluating the user's code must exist in the user's code as an exact case-sensitive match.

  0 Comments

Sign in to comment.

More Answers (1)

Jalaj Gambhir
Jalaj Gambhir on 30 Dec 2019
Hi,
As Stephen suggested, you can simply use eval or evalc with the expression as the arguement.
function SomeButtonPushed(app, event)
command = app.aTextField.Value;
% Some code to execute command
evalc(command);
end

  1 Comment

Guy Stimpson
Guy Stimpson on 30 Dec 2019
Thank you! Is it possible to get the output from evalc? So far I have not been able to obtain this and send it to my gui.

Sign in to comment.

Sign in to answer this question.