Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
declare global command

Subject: declare global command

From: wahyoe Unggul

Date: 2 May, 2011 15:33:05

Message: 1 of 9

how to declare global commands to access data on the GUI matlab?
thanks

Subject: declare global command

From: ImageAnalyst

Date: 2 May, 2011 15:54:46

Message: 2 of 9

On May 2, 11:33 am, "wahyoe Unggul" <wahyoe_slip...@yahoo.co.id>
wrote:
> how to declare global commands to access data on the GUI matlab?
> thanks

-----------------------------------------------
Commands (by this, I assume you mean "functions") are "global" if the
function is declared within the same m-file, or can be found on the
search path (see "addpath()"). To "declare" them you simply write
them (either in the same m-file or a different one on the path) - no
other "declaration" is necessary, you just start using (calling) them.

To call them with data from your GUI, you simply call them and pass in
the data you want that function to use.

Subject: declare global command

From: wahyoe Unggul

Date: 2 May, 2011 16:26:05

Message: 3 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <dfdd3fa4-c860-4e6d-a287-a4acbca1ab50@z15g2000prn.googlegroups.com>...
> On May 2, 11:33 am, "wahyoe Unggul" <wahyoe_slip...@yahoo.co.id>
> wrote:
> > how to declare global commands to access data on the GUI matlab?
> > thanks
>
> -----------------------------------------------
> Commands (by this, I assume you mean "functions") are "global" if the
> function is declared within the same m-file, or can be found on the
> search path (see "addpath()"). To "declare" them you simply write
> them (either in the same m-file or a different one on the path) - no
> other "declaration" is necessary, you just start using (calling) them.
>
> To call them with data from your GUI, you simply call them and pass in
> the data you want that function to use.

hai image
ini code saya
function inputdata_Callback(hObject, eventdata, handles)
global koordinat
[data,directory]=uigetfile({'.txt'})
f2r=fullfile(directory,data)
coord=(importdata(f2r))
set(hObject,'UserData',coord)

function plotbutton_Callback(hObject, eventdata, handles)
coord=get(handles.xj,'UserData')

while I use the "set and get" the call data, whether this can be replaced with a global command?, because in my GUI files, many use a function and I think less effective.

Subject: declare global command

From: ImageAnalyst

Date: 2 May, 2011 18:06:03

Message: 4 of 9

You can use global. Refer to teh help for details. Basically you'd
put
global coord;
in every function that wants to use it. So it's not really global,
more like a friend, where only those who ask to see it can see it --
it's not automatically available otherwise.

Subject: declare global command

From: wahyoe Unggul

Date: 2 May, 2011 18:20:23

Message: 5 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <1fc3f2d8-b34a-4f0a-96cb-8e5817aca79f@v10g2000yqn.googlegroups.com>...
> You can use global. Refer to teh help for details. Basically you'd
> put
> global coord;
> in every function that wants to use it. So it's not really global,
> more like a friend, where only those who ask to see it can see it --
> it's not automatically available otherwise.

I've tried several times but this happens
??? Undefined function or variable 'coord'

where the written code if the file GUI? I only use one GUI file
thanks

Subject: declare global command

From: ImageAnalyst

Date: 2 May, 2011 19:26:04

Message: 6 of 9

You put it inside each function, as the first line of that function.
Your coord does not need to be defined when it hits the global
command, but it should be before you leave the function.

Subject: declare global command

From: wahyoe Unggul

Date: 2 May, 2011 20:36:04

Message: 7 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7ae3cb67-9767-4252-85ba-0904bb4c60d2@z13g2000yqg.googlegroups.com>...
> You put it inside each function, as the first line of that function.
> Your coord does not need to be defined when it hits the global
> command, but it should be before you leave the function.

sorry I still do not understand, can provide a sample of the code above?
Thank you very much

Subject: declare global command

From: ImageAnalyst

Date: 2 May, 2011 21:00:23

Message: 8 of 9

Save this code as test1.m, then run it.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
function test1
    % Declare gVar global.
    % It does not need to exist or have a value yet.
    global gVar;
    gVar = 42;
    clc;
    fprintf('In test1, gVar = %d\n', gVar);
    mySubroutine1();
    mySubroutine2();
    % End of test1.

function mySubroutine1()
    % Initially, gVar is not yet seen.
    % Declare gVar global so we can see it.
    global gVar;
    % Now gVar will have the value it had in test1.
    if exist('gVar', 'var')
        fprintf('In mySubroutine1, gVar = %d\n', gVar);
    else
        fprintf('gVar does not exist in mySubroutine1.\n');
    end

function mySubroutine2()
    % No global declaration in this sub
    if exist('gVar', 'var')
        fprintf('In mySubroutine2, gVar = %d\n', gVar);
    else
        fprintf('gVar does not exist in mySubroutine2.\n');
    end

-------------------------------------------------------------------------
What you will see in the command window once you run it is this:

In test1, gVar = 42
In mySubroutine1, gVar = 42
gVar does not exist in mySubroutine2.

Subject: declare global command

From: wahyoe Unggul

Date: 2 May, 2011 21:20:08

Message: 9 of 9

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <6439fa5f-c0b0-4af4-a083-5f1eb042ff76@t19g2000prd.googlegroups.com>...
> Save this code as test1.m, then run it.
>
> % IMPORTANT: The newsreader may break long lines into multiple lines.
> % Be sure to join any long lines that got split into multiple single
> lines.
> % These can be found by the red lines on the left side of your
> % text editor, which indicate syntax errors, or else just run the
> % code and it will stop at the split lines with an error.
> function test1
> % Declare gVar global.
> % It does not need to exist or have a value yet.
> global gVar;
> gVar = 42;
> clc;
> fprintf('In test1, gVar = %d\n', gVar);
> mySubroutine1();
> mySubroutine2();
> % End of test1.
>
> function mySubroutine1()
> % Initially, gVar is not yet seen.
> % Declare gVar global so we can see it.
> global gVar;
> % Now gVar will have the value it had in test1.
> if exist('gVar', 'var')
> fprintf('In mySubroutine1, gVar = %d\n', gVar);
> else
> fprintf('gVar does not exist in mySubroutine1.\n');
> end
>
> function mySubroutine2()
> % No global declaration in this sub
> if exist('gVar', 'var')
> fprintf('In mySubroutine2, gVar = %d\n', gVar);
> else
> fprintf('gVar does not exist in mySubroutine2.\n');
> end
>
> -------------------------------------------------------------------------
> What you will see in the command window once you run it is this:
>
> In test1, gVar = 42
> In mySubroutine1, gVar = 42
> gVar does not exist in mySubroutine2.

thanks very much image

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us