- 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.
How to run a custom command from a UI
13 views (last 30 days)
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
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.
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'], ...
uicontrol('style','PushButton','units','normalize','position',[.3 .75 .2 .1],...
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+');
% Throw error if there was a problem creating/writing the m-file
error('Error accessing %s',mfileName)
% To see the new file: open(mfileName)
% make sure the m-file is on path and execute it
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