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:
Global Variable Type Declaration

Subject: Global Variable Type Declaration

From: Geoffrey

Date: 6 Oct, 2008 14:50:21

Message: 1 of 7

I'm having problems declaring global variables to be of a specific type. In one instance, when I create an array of structures and want the array to be a global variable, and the second when I have an overloaded function and pass it a declared global, but with no data in it yet.

The first is simple:
%%%% CODE %%%%
global data_Struct;
data_Struct = struct('INFO',{''},'VALUE',{});
global array_Structure;
% Everything's fine so far...
array_Structure(2) = data_Struct;
??? The following error occurred converting from struct to double: Error using ==> double
Conversion to double from struct is not possible.
%%%% \CODE %%%%

In the second instance I've got an overloaded function, the first accepts two seperate arguments as cell arrays and the second overloaded function accepts a serial object. However, when I declare the variable I want to use I don't initialize it right away and so the global variable type of double thinks i'm calling the first of the prototyped functions and forgetting to pass a second argument.

%%%% CODE %%%%
% seperate file: [status] function myfunc(cells_A,cells_B)
                          fprintf('Overloaded function1\n');
% seperate file: [status] function myfunc(serial_Obj)
                          fprintf('Overloaded function2\n');

% I've taken over the error handling for improperly passed
% arguments, so that my error messages are a little more
% user-friendly.

global serial_A;
if 0
  serial_A = serial('COM1');
end
myfunc(serial_A);
myfunc('Cereal','Eh');
%%%% CODE %%%%

It's in toggling back and forth the if statement do I see the difference: myfunc(serial_A) returns errors because it thinks i'm missing a second argument in my function call.

Long-winded question, I know. I've looked through a fair amount of posts/functions but I can't seem to find anything relating to defining global variables a little bit nicer? (Or local ones for that matter!). Does a method exist to give global variables (upon declaration) something other than the default type of double?

Thanks in advance.

Subject: Global Variable Type Declaration

From: Scott Burnside

Date: 7 Oct, 2008 03:02:19

Message: 2 of 7

"Geoffrey " <geoffrey.bourque@ch.abb.com> wrote in message <gcd8jd$m0h$1@fred.mathworks.com>...
> I'm having problems declaring global variables to be of a specific type. In one instance, when I create an array of structures and want the array to be a global variable, and the second when I have an overloaded function and pass it a declared global, but with no data in it yet.
>
> The first is simple:
> %%%% CODE %%%%
> global data_Struct;
> data_Struct = struct('INFO',{''},'VALUE',{});
> global array_Structure;
> % Everything's fine so far...
> array_Structure(2) = data_Struct;
> ??? The following error occurred converting from struct to double: Error using ==> double
> Conversion to double from struct is not possible.
> %%%% \CODE %%%%
>
> In the second instance I've got an overloaded function, the first accepts two seperate arguments as cell arrays and the second overloaded function accepts a serial object. However, when I declare the variable I want to use I don't initialize it right away and so the global variable type of double thinks i'm calling the first of the prototyped functions and forgetting to pass a second argument.
>
> %%%% CODE %%%%
> % seperate file: [status] function myfunc(cells_A,cells_B)
> fprintf('Overloaded function1\n');
> % seperate file: [status] function myfunc(serial_Obj)
> fprintf('Overloaded function2\n');
>
> % I've taken over the error handling for improperly passed
> % arguments, so that my error messages are a little more
> % user-friendly.
>
> global serial_A;
> if 0
> serial_A = serial('COM1');
> end
> myfunc(serial_A);
> myfunc('Cereal','Eh');
> %%%% CODE %%%%
>
> It's in toggling back and forth the if statement do I see the difference: myfunc(serial_A) returns errors because it thinks i'm missing a second argument in my function call.
>
> Long-winded question, I know. I've looked through a fair amount of posts/functions but I can't seem to find anything relating to defining global variables a little bit nicer? (Or local ones for that matter!). Does a method exist to give global variables (upon declaration) something other than the default type of double?
>
> Thanks in advance.
>
>
Is the use of globals really necessary? I've been working with serial and tcp/ip objects this week and just put the handles and/or data into the user-data attribute of some object (figure, button etc) that I have tagged for this purpose. You can use findobj from within subfunctions and just get the user-data attributes. I'm sure that globals have their place but I think you can do without them here.

Scott

Subject: Global Variable Type Declaration

From: Geoffrey

Date: 7 Oct, 2008 06:14:08

Message: 3 of 7

"Scott Burnside" <no@spam.com> wrote in message <gcejfr$il3$1@fred.mathworks.com>...
> Is the use of globals really necessary? I've been working with serial and tcp/ip objects this week and just put the handles and/or data into the user-data attribute of some object (figure, button etc) that I have tagged for this purpose. You can use findobj from within subfunctions and just get the user-data attributes. I'm sure that globals have their place but I think you can do without them here.
>
> Scott

From within GUIDE globals just seemed the best way to approach it. I'm sharing the serial object and structure array between multiple functions and (soon) multiple figures. Doesn't defining a global seem a simple solution? Or is MATLAB trying to shy people away from using globals by making them hard to define?

Subject: Global Variable Type Declaration

From: Bruno Luong

Date: 7 Oct, 2008 06:36:02

Message: 4 of 7

"Geoffrey " <geoffrey.bourque@ch.abb.com> wrote in message <gceung$b4i$1@fred.mathworks.com>...

> From within GUIDE globals just seemed the best way to approach it. I'm sharing the serial object and structure array between multiple functions and (soon) multiple figures. Doesn't defining a global seem a simple solution?

If you have ONE instance by application, it's OK to use global. But sorry "simple solution" is not an excuse. The functions you are developing are restricted to one global object, initialized somewhere, with certain specific calling logic of updating. Are you sure you still remember this calling logic in a year from now? Let alone someone else suppose to work in the future on your code.

Your function are bound to work that way. If later you want to adapt them from other applications, you must rewrite/modify your functions. What you create is an chunk of application code and not a usable general library.

>Or is MATLAB trying to shy people away from using globals by making them hard to define?

No. The error is not due to "global". The error is due to miss-understanding of Matlab type mechanism. This code (without global) produces the same error than yours:

data_Struct = struct('INFO',{''},'VALUE',{});
array_Structure=[];
array_Structure(2)= data_Struct;

Bruno

Subject: Global Variable Type Declaration

From: Geoffrey

Date: 7 Oct, 2008 07:07:01

Message: 5 of 7

Then how do I store information that I'd like to be globally accessible? Things like the filename of whatever file I presently have open with my GUI, this serial object I'm using, my data structure. GUIDATA?

Could someone point me in the direction of a good book to buy on the subject?

Subject: Global Variable Type Declaration

From: Bruno Luong

Date: 7 Oct, 2008 07:20:03

Message: 6 of 7

"Geoffrey " <geoffrey.bourque@ch.abb.com> wrote in message <gcf1ql$128$1@fred.mathworks.com>...
> Then how do I store information that I'd like to be globally accessible? Things like the filename of whatever file I presently have open with my GUI, this serial object I'm using, my data structure. GUIDATA?
>
> Could someone point me in the direction of a good book to buy on the subject?
>

Beside global, you have three other mechanisms to share data among GUI:

1. guidata,
2. setappdata and getappdata
3. set 'UserData' properties of your graphic objects.

What is important is also to think ahead how to structure your data.

Bruno

Subject: Global Variable Type Declaration

From: Steven Lord

Date: 7 Oct, 2008 15:50:02

Message: 7 of 7


"Geoffrey " <geoffrey.bourque@ch.abb.com> wrote in message
news:gcd8jd$m0h$1@fred.mathworks.com...
> I'm having problems declaring global variables to be of a specific type.
> In one instance, when I create an array of structures and want the array
> to be a global variable, and the second when I have an overloaded function
> and pass it a declared global, but with no data in it yet.
>
> The first is simple:
> %%%% CODE %%%%
> global data_Struct;
> data_Struct = struct('INFO',{''},'VALUE',{});
> global array_Structure;
> % Everything's fine so far...
> array_Structure(2) = data_Struct;
> ??? The following error occurred converting from struct to double: Error
> using ==> double
> Conversion to double from struct is not possible.
> %%%% \CODE %%%%

The problem here is not caused by global, at least not directly. When you
define a variable to be global, it starts out as an empty (0-by-0) double
array. So this line:

global array_Structure

sets array_Structure to be a global array containing [].

Now when you try to assign a struct array (data_Struct) into an element of
the array_Structure double variable, since the variable has to be all one
type, MATLAB has to convert one of the variables into the other's type. In
this case, since you're trying to put the struct into the double array, it
tries to convert the struct into a double array. For some pairs of the
built-in data types, MATLAB knows how to convert between the two data types
implicitly (for instance, double to single) -- but struct and double is not
one of those pairs of types, so MATLAB says that converting a struct into a
double is not possible. It doesn't know how to do it.

> In the second instance I've got an overloaded function, the first accepts
> two seperate arguments as cell arrays and the second overloaded function
> accepts a serial object. However, when I declare the variable I want to
> use I don't initialize it right away and so the global variable type of
> double thinks i'm calling the first of the prototyped functions and
> forgetting to pass a second argument.
>
> %%%% CODE %%%%
> % seperate file: [status] function myfunc(cells_A,cells_B)
> fprintf('Overloaded function1\n');
> % seperate file: [status] function myfunc(serial_Obj)
> fprintf('Overloaded function2\n');

Note that MATLAB will call whichever one of these functions is in the
current directory or appears first on the path, unless one or both are
methods of a class. MATLAB will not automatically call the first one if you
call myfunc with two inputs and the second if you call myfunc with one
input, or to put it another way dispatching to M-file functions doesn't take
the number of inputs into account.

> % I've taken over the error handling for improperly passed
> % arguments, so that my error messages are a little more
> % user-friendly.
>
> global serial_A;

Again, this assigns the empty 0-by-0 double array ([]) to serial_A.

> if 0
> serial_A = serial('COM1');
> end
> myfunc(serial_A);
> myfunc('Cereal','Eh');
> %%%% CODE %%%%
>
> It's in toggling back and forth the if statement do I see the difference:
> myfunc(serial_A) returns errors because it thinks i'm missing a second
> argument in my function call.

Correct. As I said above, MATLAB doesn't dispatch based on the number of
input arguments but on whether the function is an overloaded method and
where the function appears on the path. It's a little bit more complicated
if subfunctions or private functions are involved; the full description is
on this page from the documentation:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-58170.html#bresuvu-3

> Long-winded question, I know. I've looked through a fair amount of
> posts/functions but I can't seem to find anything relating to defining
> global variables a little bit nicer? (Or local ones for that matter!).
> Does a method exist to give global variables (upon declaration) something
> other than the default type of double?

Yes; _non-subscripted_ assignment. Subscripted assignment [like a(2) = b]
converts the right-hand side variable into the type of the left-hand side
variable; non-subscripted assignment [a = b] doesn't, it changes the type of
the left-hand side variable but doesn't affect globalness.

Still, I'd avoid globals. If you were to use global variables like your
serial_A code above, you'd have to check that all your code uses it
correctly, and all code that you call either doesn't use a global with the
same name or uses it the way you expect, and all code that those pieces of
code call behave themsevesl, and all code that the previous pieces of code
call ... it can be very difficult to debug problems like this when they
occur.

Since I see from a later post in this thread that you want to share data
between functions and GUIs, I suggest you look at the approaches described
in the documentation here:

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f5-1002439.html

and in this section (which contains the page above)

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/bqzzla9-2.html

--
Steve Lord
slord@mathworks.com

Tags for 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