How to create a user interface for a function in Matlab?

17 次查看(过去 30 天)
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 个评论
Adam
Adam 2017-7-3
"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.

请先登录,再进行评论。

回答(4 个)

ES
ES 2017-7-3
编辑:ES 2017-7-3
Contrary to popular opinion, i find MATLAB GUIde very useful to provide a GUI for my scripts/functions.
try
guide
or
appdesigner

Image Analyst
Image Analyst 2017-7-3

Jan
Jan 2017-7-3
编辑:Jan 2017-7-3
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.
  1 个评论
Lidiya P
Lidiya P 2017-7-3
Hi thank you very much! Although I'd like to learn something about programming a GUI on myself, I don't have that much time for this task, so I think I might use the tools.

请先登录,再进行评论。


dpb
dpb 2017-7-3
编辑:dpb 2017-7-3
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... :)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by