How to make a user interface using function.m
17 次查看(过去 30 天)
显示 更早的评论
#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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!