|
"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
|