2つの編集可能なテキストボックスと4つの静的テキストボックスを使用してGUIDEでMatlabGUIを作成しました。ユーザーは2つの編集可能なテキストボックス(e1
およびe2
)に値を入力し、これらの値に基づいて静的テキストボックスに表示される値を計算します(s1
、、、s2
およびs3
)s4
。
との値が変化するたびにこれを行いe1
ますe2
値を変更したときに値を計算するコードをe1
以下に示します。
% --- 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で囲まれている)を参照して、function e2_KeyPressFcn
それも使用できるようにすることは可能ですか?ここで、セクションをコピーして貼り付けますfunction e2_KeyPressFcn
が、これはあまりエレガントではないようです。