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 and structure arrays issue

Subject: Global and structure arrays issue

From: Julien Chaput

Date: 6 Jul, 2008 03:50:03

Message: 1 of 4

I've noticed that there seems to be an issue using "global"
on structure arrays, as it sets the structure to empty
despite the fact that it is not.
I'm using the function:
[A,B] = load_sac('file.sac')
which reads in a seismic data time series in "B" and the
corresponding header in "A" as a structure. When I try and
pass the header structure along as a global variable, I get
the crash notice:
"Conversion to double from struct is not possible."

Has anyone else encountered bizarre problems related to
global and structure arrays?

Thanks
Julien

Subject: Global and structure arrays issue

From: Bruno Luong

Date: 6 Jul, 2008 10:27:01

Message: 2 of 4

"Julien Chaput" <fur_ladefache_remove_this@hotmail.com>
wrote in message <g4pfdb$ofc$1@fred.mathworks.com>...
> I've noticed that there seems to be an issue using "global"
> on structure arrays, as it sets the structure to empty
> despite the fact that it is not.

Yes it is initialize as empty array, because MATLAB does not
know in advance what will be stored inside, it can be
anything, structure, numerical array, cell, function handle,
... you name it.

> I'm using the function:
> [A,B] = load_sac('file.sac')
> which reads in a seismic data time series in "B" and the
> corresponding header in "A" as a structure. When I try and
> pass the header structure along as a global variable, I get
> the crash notice:
> "Conversion to double from struct is not possible."
>

This is not due to global, it due to how you assign value to
the structure array

% global fool;
fool = []; % <- not necessary
fool(1) = struct('myfield', 1); % <- error


% global fool;
fool = []; % <- not necessary
if ~isempty(fool) && isstruct(fool)
   fool(end+1) = struct('myfield', 2);% make sure compatibility
else % initialize
   fool = struct('myfield', 1);
end

Bruno

Subject: Global and structure arrays issue

From: Julien Chaput

Date: 7 Jul, 2008 02:36:02

Message: 3 of 4

Huh; interesting. Thanks for the tip, I think it works now,
though I'm still a little confused as to why the first few
lines you wrote down doesn't work.
Thanks a bunch.

Julien

Subject: Global and structure arrays issue

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 7 Jul, 2008 02:53:11

Message: 4 of 4

In article <g4rvei$n10$1@fred.mathworks.com>,
Julien Chaput <fur_ladefache_remove_this@hotmail.com> wrote:
>Huh; interesting. Thanks for the tip, I think it works now,
>though I'm still a little confused as to why the first few
>lines you wrote down doesn't work.

Those "first few lines" appear to have been,

% global fool;
fool = []; % <- not necessary
fool(1) = struct('myfield', 1); % <- error


After the fool = [] line, fool will be a 0 x 0 array of double.
The "fool(1) =" line attempts to assign a value to the first
element of that array. When you assign to a subscript of the array,
the array cannot change its basic type, so if the assignment
were to be able to succeed, then whatever was being assigned
to fool(1) would have to be of the existing type of fool's array
elements, namely double. But a struct, no matter what its fields,
is -not- of type double, so Matlab attempts to find a way to
convert the struct to type double, just the way it would look for
a way to convert a character to double if you had attempted
to assign fool(1) = 'H'. Matlab does know how to convert char
to double, but it doesn't know any way to convert entire structs
to type double, so it fails on the assignment complaining that
it cannot convert struct to double, the message you saw.

The basic lesson you have to remember here is that [] does not
represent a -typeless- empty array, it represents an empty
array of type double, and when you assign [] to a variable,
that variable become type double. Matlab doesn't have any
kind of provision for a variable (or field) which
"exists and is empty but is not of any particular type."
--
  "All human knowledge takes the form of interpretation."
                                              -- Walter Benjamin

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