I have made a Matlab GUI in GUIDE with two editable text boxes and four static text boxes
The user inputs values in the two editable text boxes (e1 and e2) and based on these values it calculates the values that should be displayed in the static text boxes (s1, s2, s3 and s4).
It does this on each value change of e1 and e2
The code to calculate the values when e1 changes value is shown below.
% --- 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
Is it possible to reference this block of code (enclosed in BLOCK) such that function e2_KeyPressFcn can use it as well?
Now I would just copy paste the section to function e2_KeyPressFcn but this seems not very elegant.
How about making a helper function for your block of code?
I’m thinking something along these lines: