How to create a user interface for a function in Matlab?
17 views (last 30 days)
Show older comments
Hi,
I have a function which requires some parameters. Now, I want to make a user interface, so the user does not have to change the parameters by changing the code but has just to fill in some field and press start or something. Can somebody give me an idea on how to do that in matlab? I didn't have to do anything with GUI's or user interfaces before so i don't really know where to start.
8 Comments
Adam
on 3 Jul 2017
"I understand, but then you'd still have to use the Matlab code to change the parameters. I want to make this userface because my function is supposed to be used by people who don't know matlab at all."
Do you mean you want people who don't even have access to Matlab to be able to use it as an executable or just that people shouldn't need to have to write Matlab statements to use it?
If you want to build a standalone executable you would need the Matlab Compiler toolbox. Doesn't make a difference to the GUI you would build except that if your only reason to build one was for external users and you don't have the Compiler toolbox it may change your decision on if it is needed or not.
Arnab Banerjee
on 2 Nov 2019
this link may solve your problem, same for other visual explorer..
Answers (4)
Jan
on 3 Jul 2017
Edited: Jan
on 3 Jul 2017
You can either use the tools GUIDE or APPDesigner, or create the GUI programatically. It matters if you want to learn something about the creation of GUIs or need a fast and cheap solution only.
This would be a way to start for a programatical solution:
function yourGUI % Use a better name, of course
DlgH = figure( ...
'Name', 'YourGUI', ...
'IntegerHandle', 'off', ...
'WindowStyle', 'normal', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Resize', 'off', ...
'Units', 'pixels', ...
'Position', [100, 100, 400, 300], ...
'NextPlot', 'add');
uicontrol('Style', 'PushButton', 'String', 'Run function', ...
'Position', [10, 10, 380, 25], ...
'Callback' @RunFunction);
UIData = guidata(ButtonH););
UIData.Param1 = uicontrol('Style', 'edit', 'String', '', ...
'Position', [10, 360, 300, 25]);
UIData.FileSelect = uicontrol('Style', 'PushButton', ...
'String', '...', 'Callback', @FileSelect, ...
'Position', [320, 360, 80, 25]);
UIData.Param2 = uicontrol('Style', 'edit', 'String', '', ...
'Position', [10, 360, 300, 25], ...
'Callback', @CheckValue);
guidata(FlgH, UIData);
end
function FileSelect(ButtonH, EventData)
UIData = guidata(ButtonH);
[FileName, FilePath] = uigetfile('*.xlsx', 'Select an Excel file');
if ischar(FileName);
File = fullfile(FilePath, FileName);
set(UIData.Param1, 'String', File);
end
end
function CheckValue(EditH, EventData)
Str = get(EditH, 'String');
Num = sscanf(Str, '%g', 1);
if isempty(Num)
set(EditH, 'String', 'Enter a number!', ...
'ForeGroundColor', [1,0,0], ...
'UserData', []); % Invalid
else
set(EditH, 'String', sprintf('%g', Num), ...
'ForeGroundColor', [0,0,0], ...
'UserData', true); % Valid number
end
end
function RunFunction(ButtonH, EventData)
UIData = guidata(ButtonH);
if isempty(get(UIData.Param2, 'UserData'))
set(UIData.Param2, 'BackGroundColor', [1,0,0]);
pause(0.5);
set(UIData.Param2, 'BackGroundColor', [1,1,1]);
return;
end
File = get(UIData.Param1);
Value = sscanf(get(UIData.Param2, 'String'), '%g', 1);
% Now run your function:
yourFunction(File, Value);
end
UNTESTED CODE! This was written in the forum's interface only. Please debug this by your own. I assume the layout is cruel, but you can adjust the 'Position' properties manually.
dpb
on 3 Jul 2017
Edited: dpb
on 3 Jul 2017
Or, just use the builtin widgets interface to get the parameters may be enough...
fn=uigetfile('*.xls','Select Input File');
data=xlsread(fn);
P1=str2num(inputdlg('Enter Parameter 1'));
P2=str2num(inputdlg('Enter Parameter 2'));
output = yourfunction(data,[P1,P2]);
Needs some error checking and perhaps an outer loop but is pretty quick 'n dirty and may get your users started this morning... :)
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!