How to make a user interface using function.m

조회 수: 17 (최근 30일)
Sihem
Sihem 2022년 7월 23일
편집: Sihem 2022년 10월 7일
#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.

채택된 답변

Voss
Voss 2022년 7월 23일
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
Voss
Voss 2022년 7월 24일
편집: Voss 2022년 7월 24일
Excellent! You're welcome!
Sihem
Sihem 2022년 10월 7일
편집: Sihem 2022년 10월 7일
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?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by