4

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.

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51

1 Answers1

2

How about making a helper function for your block of code?

I'm thinking something along these lines:

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'))));
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • Could I place the function `hout` in a separate file? I've tried it now but it doesn't work. Should I use some special naming? – Saaru Lindestøkke Dec 18 '12 at 14:12
  • 1
    @BartArondson I called the helper function `helper_block_func`... `hout` is not the function name, it's a local variable in that function. Yes, of course you can put it in a separate m-file, just make sure you the file is named like the function (for example "helper_block_func.m") – Eitan T Dec 18 '12 at 15:28