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

조회 수: 43 (최근 30일)
Lidiya P
Lidiya P 2017년 7월 3일
댓글: Arnab Banerjee 2019년 11월 2일
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.
Arnab Banerjee
Arnab Banerjee 2019년 11월 2일
this link may solve your problem, same for other visual explorer..

댓글을 달려면 로그인하십시오.

답변 (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