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:
Trouble initializing struct the way I want

Subject: Trouble initializing struct the way I want

From: Jeff

Date: 7 Feb, 2011 10:52:04

Message: 1 of 4

How do I initialize this so that all the fields of the structure are null, except for the index (idx) which should equal 1?

I've tried:
s.xtrma=[];
...
s.idx=1;
freq(nFreqs)=s

But that only set the last frequency's index to 1. The rest were null.

I tried:

freq(nFreqs).xtrma=[];
freq(nFreqs).idx=1;
Generates an "assignment between unlike types" error.

freq(nFreqs).xtrma=[];
freq(nFreqs).idx=1;
Generates an "assignment between unlike types" error.

freq(nFreqs).xtrma=[];
freq(nFreqs).idx=[1 1 1 1 1 1 1 1 1];
Somehow this does not cause an error, but it puts an array of nine ones in the last frequency (which is nine, in this case), when I wanted a one in all nine frequencies.

freq(nFreqs).xtrma=[];
freq(nFreqs).idx={1 1 1 1 1 1 1 1 1};
Almost the same as above, except freq(1,9).idx is a cell array of nine ones.

freq(nFreqs).xtrma=[];
freq(nFreqs).idx={1,1,1,1,1,1,1,1,1};
Puts the whole cell in the last (ninth) frequency.

This works, but I have more than just these two variables (also, I don't know yet how to dynamically set the size of the cell array of ones):
freq=struct('xtrma', [], 'idx', {1,1,1,1,1,1,1,1,1})

I've read the help file on struct and this article: http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/. But I can't figure out how to preallocate all idx to 1.

Can anyone help? Thanks.
Jeff

Subject: Trouble initializing struct the way I want

From: Jan Simon

Date: 7 Feb, 2011 13:06:03

Message: 2 of 4

Dear Jeff,

> s.xtrma=[];
> ...
> s.idx=1;
> freq(nFreqs)=s
>
> But that only set the last frequency's index to 1. The rest were null.

  nFreqs = 10;
  freq = struct('xtrma', [], 's', cell(1, nFreqs));
  [freq.s] = deal(1);
==>
  freq(10)
  >> ans =
    xtrma: []
    s: 1

Another approach - I assume it is faster:
  C = cell(1, nFreqs);
  C(:) = {1};
  freq = struct('xtrma', [], 's', C);

Does this help? Jan

Subject: Trouble initializing struct the way I want

From: Bruno Luong

Date: 7 Feb, 2011 13:09:03

Message: 3 of 4

One way:

% clear s
s(1:10) = struct('xterma',[],'idx',[]);
[s.idx] = deal(1);

% Bruno

Subject: Trouble initializing struct the way I want

From: Jeff

Date: 8 Feb, 2011 21:38:03

Message: 4 of 4

"Jan Simon" wrote in message <iioqnr$5le$1@fred.mathworks.com>...
> Dear Jeff,
>
> > s.xtrma=[];
> > ...
> > s.idx=1;
> > freq(nFreqs)=s
> >
> > But that only set the last frequency's index to 1. The rest were null.
>
> nFreqs = 10;
> freq = struct('xtrma', [], 's', cell(1, nFreqs));
> [freq.s] = deal(1);
> ==>
> freq(10)
> >> ans =
> xtrma: []
> s: 1
>
> Another approach - I assume it is faster:
> C = cell(1, nFreqs);
> C(:) = {1};
> freq = struct('xtrma', [], 's', C);
>
> Does this help? Jan

Yes. Deal works (I hadn't heard of the command).
Your second method works, too. But I'll stick with Deal (at least for now).

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <iioqtf$glh$1@fred.mathworks.com>...
> One way:
>
> % clear s
> s(1:10) = struct('xterma',[],'idx',[]);
> [s.idx] = deal(1);
>
> % Bruno

Thanks.

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