How to make a user interface using function.m

17 visualizaciones (últimos 30 días)
Sihem
Sihem el 23 de Jul. de 2022
Editada: Sihem el 7 de Oct. de 2022
#Help please
Hello, hope that you're in a good health
I have 4 functions coded in matlab, and i want to create an interface that shows the result of each function after clicking on the coressponding button (when i click on function 1 his result appears in an 'edit text' ), can any one tell me how to do this?
I hope you understend.

Respuesta aceptada

Voss
Voss el 23 de Jul. de 2022
Here is some code you can run, refer to, and possibly use for your purpose.
I wasn't sure how many inputs your functions take or where the inputs come from, so here I've made these functions take a single input which can be input in an edit box in the GUI. Note that when the input value changes, the function values automatically update, so there is no need to click the individual buttons (which means the buttons could be removed or replaced with static text boxes). You may or may not want this behavior in your GUI, depending on, say, how long it takes your functions to run.
function function_results()
funcs = {@sin @cos @tan @(x)x^2};
f = figure( ...
'Units','pixels', ...
'Name','Function Results', ...
'IntegerHandle','off', ...
'HandleVisibility','off', ...
'NumberTitle','off', ...
'DockControls','off', ...
'Menubar','none', ...
'Toolbar','none');
n_funcs_given = numel(funcs);
x_text = uicontrol( ...
'Parent',f, ...
'Units','pixels', ...
'Style','text', ...
'String','x:', ...
'HorizontalAlignment','right');
x_edit = uicontrol( ...
'Parent',f, ...
'Units','pixels', ...
'Style','edit', ...
'String','0', ...
'Callback',@cb_x_edit);
buttons = zeros(1,n_funcs_given);
edits = zeros(1,n_funcs_given);
for ii = 1:n_funcs_given
buttons(ii) = uicontrol( ...
'Parent',f, ...
'Units','pixels', ...
'Style','pushbutton', ...
'String',m_func2str(funcs{ii}), ...
'Callback',@cb_button);
edits(ii) = uicontrol( ...
'Parent',f, ...
'Units','pixels', ...
'Style','edit', ...
'String','', ...
'Enable','inactive');
end
fpos = get(f,'Position');
new_height = 30*n_funcs_given+15;
fpos(2) = fpos(2)+fpos(4)-new_height;
fpos(3) = 238;
fpos(4) = new_height;
set(f,'SizeChangedFcn',@scf,'Position',fpos);
clear('ii','fpos','new_height');
set_result_str();
function cb_button(src,~)
set_result_str(find(src == buttons));
end
function cb_x_edit(~,~)
set_result_str();
end
function set_result_str(idx)
if ~nargin
idx = 1:n_funcs_given;
end
x = str2double(get(x_edit,'String'));
for jj = 1:numel(idx)
set(edits(idx(jj)),'String',num2str(funcs{idx(jj)}(x)));
end
end
function scf(~,~)
pos = get(f,'Position');
yy = pos(4)-30;
set(x_text,'Position',[10 yy 16 18]);
set(x_edit,'Position',[30 yy 44 20]);
ww = max(0,pos(3)-184);
for idx = 1:n_funcs_given
set(buttons(idx),'Position',[104 yy 66 20]);
set(edits(idx),'Position',[174 yy ww 20]);
yy = yy-30;
end
end
function str = m_func2str(func)
str = func2str(func);
if startsWith(str,'@(x)')
str = str(5:end);
end
end
end
  9 comentarios
Voss
Voss el 24 de Jul. de 2022
Editada: Voss el 24 de Jul. de 2022
Excellent! You're welcome!
Sihem
Sihem el 7 de Oct. de 2022
Editada: Sihem el 7 de Oct. de 2022
Hello, i wish that you're in a good health, can you help me please? this is the last step in my GUI i have 2 questions:
1- How can i return a value from a check box or radio button for exemple according to the figure i want when i select the first checkbox i return m =3?
2 - As you see in the figure bellow, when i run the GUI, some checkbox disappear, what's the problem?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by