Question:
I am making a GUI in matlab. How do I access the same variable from different functions?
take_me_to_ur_leader
2009-12-28 04:03:51 UTC
After a lot of googling.. I came up with 2 things:
(1) global variable
(2) handles
the global thing isn't working..
global r;
r = imread('Blue hills.jpg');
then in another function when i used r, it says undefined function or variable.

The other option is handles. Plz explain how i can use handles to access the same variable from different functions. Thanks.
Five answers:
MyKidsFan
2009-12-28 07:37:15 UTC
Global should work easily. Did you say "global r" in the other function that is also using r? Globals must be declared Inside each function that will be using it, not just the file.
anonymous
2009-12-28 16:43:22 UTC
There are three very important instructions when working with GUIs: ‘get’, ‘guidata’, and ‘set’.



The ‘get’ instruction, gets the string value from an input component. For example, if we want to get the number that the user inputs in ‘edit1’, we can do it like this (preceed the identifier tag with ‘handles.’):



get(handles.edit1, 'String')



This instruction gets a string, not a number; thus, if we need to numerically manipulate that value, we have to transform it into a number first. For example, something typical is:



num = str2double(get(handles.edit1,'String'));



Now, our variable ‘num’ does contain a number (double), and we can manipulate it.



You must update the value of the component and keep that variable in the memory-workspace (using the ‘guidata’ instruction) to be used in other callback functions. We can achieve it by doing this:



handles.edit1 = num;

guidata(hObject,handles)



Generally speaking, we need to finish every callback function with



guidata(hObject,handles)



in order to maintain in memory the values of the variables.



I suggest you visit the site below.

Good luck!
?
2016-12-16 22:43:24 UTC
Global Variable Matlab Gui
anonymous
2016-03-14 05:46:31 UTC
Having access to information does not mean that people will read or understand the information. Plus, you have two different modes of thinking between the scientific and religious mindsets. The scientist would look at the evidence and try to explain it as best as possible. The theist would take his book or set of beliefs and since the conclusion is already in hand, they must bend and misrepresent the information and evidence that exists to fit their hope. Anyways, the point is, having access does not mean that people will utilize said access and learn. Cheers
Nasir Hussain
2014-10-16 08:54:24 UTC
a simple gui program to show how to use global variable





function varargout = Global_var_func(varargin)

% GLOBAL_VAR_FUNC MATLAB code for Global_var_func.fig

% GLOBAL_VAR_FUNC, by itself, creates a new GLOBAL_VAR_FUNC or raises the existing

% singleton*.

%

% H = GLOBAL_VAR_FUNC returns the handle to a new GLOBAL_VAR_FUNC or the handle to

% the existing singleton*.

%

% GLOBAL_VAR_FUNC('CALLBACK',hObject,eventData,handles,...) calls the local

% function named CALLBACK in GLOBAL_VAR_FUNC.M with the given input arguments.

%

% GLOBAL_VAR_FUNC('Property','Value',...) creates a new GLOBAL_VAR_FUNC or raises the

% existing singleton*. Starting from the left, property value pairs are

% applied to the GUI before Global_var_func_OpeningFcn gets called. An

% unrecognized property name or invalid value makes property application

% stop. All inputs are passed to Global_var_func_OpeningFcn via varargin.

%

% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one

% instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES



% Edit the above text to modify the response to help Global_var_func



% Last Modified by GUIDE v2.5 16-Oct-2014 20:50:13



% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @Global_var_func_OpeningFcn, ...

'gui_OutputFcn', @Global_var_func_OutputFcn, ...

'gui_LayoutFcn', [] , ...

'gui_Callback', []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

end



if nargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT





% --- Executes just before Global_var_func is made visible.

function Global_var_func_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to Global_var_func (see VARARGIN)



% Choose default command line output for Global_var_func

handles.output = hObject;



global a b;

a=1:100;

b=sin(a);



% Update handles structure

guidata(hObject, handles);



% UIWAIT makes Global_var_func wait for user response (see UIRESUME)

% uiwait(handles.figure1);





% --- Outputs from this function are returned to the command line.

function varargout = Global_var_func_OutputFcn(hObject, eventdata, handles)

% varargout cell array for returning output args (see VARARGOUT);

% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)



% Get default command line output from handles structure

varargout{1} = handles.output;





% --- Executes on button press in pushbutton1.

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global a b;

plot(a,b);











May help you...............


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...