How to make a user interface using function.m

17 visualizzazioni (ultimi 30 giorni)
#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.

Risposta accettata

Voss
Voss il 23 Lug 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 Commenti
Voss
Voss il 24 Lug 2022
Modificato: Voss il 24 Lug 2022
Excellent! You're welcome!
Sihem
Sihem il 7 Ott 2022
Modificato: Sihem il 7 Ott 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?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by