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

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

Stephen23
Stephen23 el 3 de Jul. de 2017
Editada: Stephen23 el 3 de Jul. de 2017
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
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.
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.
Hey Jan Simon,
I don't have any requirements for the look of the userface, it is just supposed to work. My function has two variables: I have to import an excel sheet so the excel sheet name is one parameter, another is a numeric value. Also, I have an excel sheet as an output, so I don't need the interface to have an output.
Lidiya P
Lidiya P el 3 de Jul. de 2017
Editada: Lidiya P el 3 de Jul. de 2017
Something like that
Jan
Jan el 3 de Jul. de 2017
Fine!
Adam
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.
this link may solve your problem, same for other visual explorer..

Iniciar sesión para comentar.

Respuestas (4)

ES
ES el 3 de Jul. de 2017
Editada: ES el 3 de Jul. de 2017
Contrary to popular opinion, i find MATLAB GUIde very useful to provide a GUI for my scripts/functions.
try
guide
or
appdesigner
Jan
Jan el 3 de Jul. de 2017
Editada: Jan el 3 de Jul. de 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.

1 comentario

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.

Iniciar sesión para comentar.

dpb
dpb el 3 de Jul. de 2017
Editada: dpb 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.

Etiquetas

Preguntada:

el 3 de Jul. de 2017

Comentada:

el 2 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by