Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:37:30+00:00 2026-06-16T04:37:30+00:00

I have made a Matlab GUI in GUIDE with two editable text boxes and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:37:31+00:00Added an answer on June 16, 2026 at 4:37 am

    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'))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Guide I made a Matlab Gui that have 10 Axes in which i
I have GUI in Matlab that I have made using the Programmatic approach. It
I have made an iphone application like flip cards in which values of cards
I have made a list based navigation bar for my newest project and I
I am learning matlab myself andI have made an animated plot using matlab;now i
I am using Liblinear in matlab to predict values, I have some classes and
I have made a login page. When the user logs in a request to
I have made a plot in Matlab, using: hold on plot(t1,Dx1,'r') xlabel('t (ps)') ylabel('Deviation
I have made a jQuery toggle for a menu that I had in mind.
I have made an application for IPad in objective C. In this I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.