Tuesday, 3 December 2013

How to handle variable from one function to other function in MATLAB GUI?


Handling variable from one function to other function is also an important job. Instead of taking variables once again from user, anyone can use saved variables by handling them properly in function to function. This process will reduce code length in MATLAB.  


Illustration:
Suppose we want to ADD two numbers. After that our aim is to find an average of these two numbers in GUI. This can be achieved by using following fig file as shown in fig 1.


Fig 1: Fig file with ADD and Average Operation

1. Our aim is to handle two variables 1st number and 2nd number under ADD button. 

2. Under AVG button we directly handle addition of two numbers that we already done under ADD button. Instead of handling two numbers once again under AVG button we directly take addition of two numbers form function of ADD button this can be shown in following fig 2.

Syntax:

function 1
A=10;
handles.A=A;
guidata(hObject, handles);
end

function 2
A=handles.A;
B=A;
end

3. Now we just apply this syntax for variable handling from Pushbutton1 to pushbutton2 in m file as shown in following fig 2. 
 

Fig 2: m file with Variable handling


   Fig 3: Final Output after clicking on ADD


  Fig 4: Final Output after clicking on AVG

 

No comments:

Post a comment