How to make a user interface using function.m
显示 更早的评论
#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.
采纳的回答
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 个评论
i didn't understend ): , i have 4 functions which represent 2 heuristics and 2 meta-heuristics (simulated anealing and genetic algorithm), the main problem that i have is that when i push the button i have no result in the edit text , what's the instruction that i must write to show the result in the corresponding edit.
A button's 'Callback' is a function that is executed when the button is pressed. If you have not done so already, you'll have to define 'Callback' functions for your buttons. Then in each callback you'll set the 'String' property of the corresponding edit box.
If you think you have done those things and it's not working, share your code.
function varargout = Interface_essay(varargin)
% INTERFACE_ESSAY MATLAB code for Interface_essay.fig
% INTERFACE_ESSAY, by itself, creates a new INTERFACE_ESSAY or raises the existing
% singleton*.
%
% H = INTERFACE_ESSAY returns the handle to a new INTERFACE_ESSAY or the handle to
% the existing singleton*.
%
% INTERFACE_ESSAY('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in INTERFACE_ESSAY.M with the given input arguments.
%
% INTERFACE_ESSAY('Property','Value',...) creates a new INTERFACE_ESSAY or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Interface_essay_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Interface_essay_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Interface_essay
% Last Modified by GUIDE v2.5 23-Jul-2022 21:19:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Interface_essay_OpeningFcn, ...
'gui_OutputFcn', @Interface_essay_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Interface_essay is made visible.
function Interface_essay_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Interface_essay (see VARARGIN)
% Choose default command line output for Interface_essay
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Interface_essay wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Interface_essay_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes when entered data in editable cell(s) in TableField.
function TableField_CellEditCallback(hObject, eventdata, handles, varargin)
global p
p.MyData =[];
% hObject handle to TableField (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
handles.output =hObject;
guidata(hObject, handles);
function SetupField_Callback(hObject, eventdata, handles)
% hObject handle to SetupField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of SetupField as text
% str2double(get(hObject,'String')) returns contents of SetupField as a double
% --- Executes during object creation, after setting all properties.
function SetupField_CreateFcn(hObject, eventdata, handles)
% hObject handle to SetupField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function PeriodField_Callback(hObject, eventdata, handles)
% hObject handle to PeriodField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of PeriodField as text
% str2double(get(hObject,'String')) returns contents of PeriodField as a double
% --- Executes during object creation, after setting all properties.
function PeriodField_CreateFcn(hObject, eventdata, handles)
% hObject handle to PeriodField (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in heuristique1.
function heuristique1_Callback(hObject, eventdata, handles)
% hObject handle to heuristique1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
heuristique1([ 12 6 3 8 17 20 18 2 7 5], [ 7 20 11 15 17 4 19 3 15 15],3);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes when entered data in editable cell(s) in uitable3.
function uitable3_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable3 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
str2double(get(C_max,'String'))
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit6_Callback(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit6 as text
% str2double(get(hObject,'String')) returns contents of edit6 as a double
% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit7_Callback(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit7 as text
% str2double(get(hObject,'String')) returns contents of edit7 as a double
% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
it shows the result in the workspace not in the edit text
I gather that you're referring to heuristique1_Callback, and you want to show the result of the call to the function heuristique1 in an edit box.
In that case, in heuristique1_Callback, you'd say:
result = heuristique1([ 12 6 3 8 17 20 18 2 7 5], [ 7 20 11 15 17 4 19 3 15 15],3);
set(handles.edit4,'String',num2str(result));
That places the result in handles.edit4 (but I don't know if that's the correct edit box, so if not, change it to the correct one).
(Also, by the way, edit4_Callback will give you an error if it executes:
str2double(get(C_max,'String'))
because C_max is not defined.)
oooh thank you sooo much it works now.
Excellent! You're welcome!
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Argument Definitions 的更多信息
标签
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
