How to create a user interface for a function in Matlab?
Mostrar comentarios más antiguos
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 comentarios
Creating a UI can be a lot of work: do you absolutely need to create one? How many parameter values are there?
It may be much simpler to turn your script into a function and then it can be called very simply with those parameter values, e.g.:
name = 'anna';
time = '2017-07-03T12:22';
dist = 23.5;
myfun(time, dist, name)
Jan
el 3 de Jul. de 2017
Please explain the problem with more details: Do you want edit fields or sliders to define the parameters? How many are "some"? While "press start" is something we can suggest some GUI elements and code for, "or something" is impossible - it is your turn to decide and explain, what you want.
Do you know user interfaces from any other programs? You can design the interface on paper at first and post a photo.
Lidiya P
el 3 de Jul. de 2017
Lidiya P
el 3 de Jul. de 2017
Jan
el 3 de Jul. de 2017
Fine!
Adam
el 3 de Jul. de 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
el 2 de Nov. de 2019
this link may solve your problem, same for other visual explorer..
Respuestas (4)
Contrary to popular opinion, i find MATLAB GUIde very useful to provide a GUI for my scripts/functions.
try
guide
or
appdesigner
Image Analyst
el 3 de Jul. de 2017
1 voto
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 comentario
Lidiya P
el 3 de Jul. de 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... :)
Categorías
Más información sobre MATLAB Compiler en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
