matlab功能块,Matlab GUI重用功能块
我在GUIDE中創(chuàng)建了一個(gè)帶有兩個(gè)可編輯文本框和四個(gè)靜態(tài)文本框的Matlab GUI
用戶在兩個(gè)可編輯的文本框(e1和e2)中輸入值,并根據(jù)這些值計(jì)算應(yīng)在靜態(tài)文本框(s1,s2,s3和s4)中顯示的值.
它在e1和e2的每個(gè)值變化上執(zhí)行此操作
e1更改值時(shí)計(jì)算值的代碼如下所示.
% --- Executes on key press with focus on e1 and none of its controls.
function e1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to e1 (see GCBO)
% eventdata structure with the following fields (see UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
% Start of BLOCK
% Get values from e1 and e2 and calculate other values
handles.levels = str2num(get(handles.e1, 'String'));
handles.edgelength = str2num(get(handles.e2, 'String'));
handles.cellnum = (handles.levels^3 + 3*handles.levels^2 + 2*handles.levels)/6;
handles.vertnum = ((handles.levels+1)^3 + 3*(handles.levels+1)^2 + 2*(handles.levels+1))/6;
% Set values of s1, s2, s3 and s4
set(handles.s1, 'String', num2str(handles.cellnum));
set(handles.s2, 'String', num2str(handles.vertnum));
set(handles.s3, 'String', num2str(0.433*handles.edgelength^2));
set(handles.s4, 'String', ...
num2str(2*handles.cellnum*str2num(get(handles.s3, 'String'))));
% End of BLOCK
是否可以引用這段代碼(包含在BLOCK中),以便函數(shù)e2_KeyPressFcn也可以使用它?
現(xiàn)在我只是將該部分復(fù)制粘貼到函數(shù)e2_KeyPressFcn,但這似乎不是很優(yōu)雅.
最佳答案 如何為您的代碼塊創(chuàng)建幫助函數(shù)?
我正在考慮以下幾點(diǎn):
function e1_KeyPressFcn(hObject, eventdata, handles)
handles = helper_block_func(handles);
function e2_KeyPressFcn(hObject, eventdata, handles)
handles = helper_block_func(handles);
function hout = helper_block_func(hin)
hout = hin;
% # Get values from e1 and e2 and calculate other values
hout.levels = str2num(get(hout.e1, 'String'));
hout.edgelength = str2num(get(hout.e2, 'String'));
hout.cellnum = (hout.levels ^ 3 + 3 * hout.levels ^ 2 + 2 * hout.levels) / 6;
hout.vertnum = ((hout.levels + 1) ^ 3 + 3 * (hout.levels + 1) ^ 2 ...
+ 2 * (hout.levels + 1)) / 6
% # Set values of s1, s2, s3 and s4
set(hout.s1, 'String', num2str(hout.cellnum));
set(hout.s2, 'String', num2str(hout.vertnum));
set(hout.s3, 'String', num2str(0.433 * hout.edgelength ^ 2));
set(hout.s4, 'String', ...
num2str(2 * hout.cellnum * str2num(get(hout.s3, 'String'))));
總結(jié)
以上是生活随笔為你收集整理的matlab功能块,Matlab GUI重用功能块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#游戏客户端编程
- 下一篇: 第一季4:Hi3518E_SDK_Vx.