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:
How to assign a vector to a structure?

Subject: How to assign a vector to a structure?

From: Kian

Date: 14 Jun, 2009 23:37:01

Message: 1 of 8

I have a structure and an array of values that I'd like to assign to that structure. Example:

I have 10 electrodes. Each one have a number and each one has a bankID, etc. The electrode numbers go, for example, 1:10. The bankID goes, 1 & 2 for each 5 electrodes.

numbers = 1:10;
bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];

How do I assign these two arrays to these structures without a for-loop?
myStruct.electrode.number
myStruct.electrode.bankID

I'd think a command like this would do the job, but it doesn't:

myStruct.electrode(1:10).number = numbers;
myStruct.electrode(1:10).bankID = bankID;

So instead, I use:

for i = 1:10
   myStruct.electrode(i).number = numbers(i);
   myStruct.electrode(i).bankID = bankID(i);
end

I have a feeling there should be a solution involving brackets, but I can't figure it out. Alternatively, somehow converting the arrays (i.e. numbers and bankID) to structures may also do the job.

Thanks in advance!
  

Subject: How to assign a vector to a structure?

From: Sadik

Date: 15 Jun, 2009 00:22:01

Message: 2 of 8

Hi Kian,

I tried to make it one line:

s = cell2struct([mat2cell((1:10)',10,1) mat2cell([ones(5,1);2*ones(5,1)],10,1)],{'number','bankID'},2);





"Kian " <kian.torab@utah.edu> wrote in message <h141it$d34$1@fred.mathworks.com>...
> I have a structure and an array of values that I'd like to assign to that structure. Example:
>
> I have 10 electrodes. Each one have a number and each one has a bankID, etc. The electrode numbers go, for example, 1:10. The bankID goes, 1 & 2 for each 5 electrodes.
>
> numbers = 1:10;
> bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];
>
> How do I assign these two arrays to these structures without a for-loop?
> myStruct.electrode.number
> myStruct.electrode.bankID
>
> I'd think a command like this would do the job, but it doesn't:
>
> myStruct.electrode(1:10).number = numbers;
> myStruct.electrode(1:10).bankID = bankID;
>
> So instead, I use:
>
> for i = 1:10
> myStruct.electrode(i).number = numbers(i);
> myStruct.electrode(i).bankID = bankID(i);
> end
>
> I have a feeling there should be a solution involving brackets, but I can't figure it out. Alternatively, somehow converting the arrays (i.e. numbers and bankID) to structures may also do the job.
>
> Thanks in advance!
>

Subject: How to assign a vector to a structure?

From: Kian

Date: 15 Jun, 2009 03:56:01

Message: 3 of 8

Sadik,

Thanks for your response, but that doesn't achieve what I wanted to do. What you did is done by:

s.number = numbers';
s.bankID = bankID';

I want the following:

s(1).number = 1
s(3).bankID = 1
s(7).bankID = 2
and so on...

Thanks!
Kian

Subject: How to assign a vector to a structure?

From: Sadik

Date: 15 Jun, 2009 04:20:17

Message: 4 of 8

You are right, sorry. This one should work:

s = struct('number', mat2cell((1:10)',ones(10,1),1), 'bankID', mat2cell([ones(5,1);2*ones(5,1)],ones(10,1),1));



"Kian " <kian.torab@utah.edu> wrote in message <h14goh$6lc$1@fred.mathworks.com>...
> Sadik,
>
> Thanks for your response, but that doesn't achieve what I wanted to do. What you did is done by:
>
> s.number = numbers';
> s.bankID = bankID';
>
> I want the following:
>
> s(1).number = 1
> s(3).bankID = 1
> s(7).bankID = 2
> and so on...
>
> Thanks!
> Kian

Subject: How to assign a vector to a structure?

From: Bruno Luong

Date: 15 Jun, 2009 06:53:01

Message: 5 of 8

"Kian " <kian.torab@utah.edu> wrote in message <h141it$d34$1@fred.mathworks.com>...
> I have a structure and an array of values that I'd like to assign to that structure. Example:
>
> I have 10 electrodes. Each one have a number and each one has a bankID, etc. The electrode numbers go, for example, 1:10. The bankID goes, 1 & 2 for each 5 electrodes.
>
> numbers = 1:10;
> bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];
>
> How do I assign these two arrays to these structures without a for-loop?
> myStruct.electrode.number
> myStruct.electrode.bankID
>
> I'd think a command like this would do the job, but it doesn't:
>
> myStruct.electrode(1:10).number = numbers;
> myStruct.electrode(1:10).bankID = bankID;
>
> So instead, I use:
>
> for i = 1:10
> myStruct.electrode(i).number = numbers(i);
> myStruct.electrode(i).bankID = bankID(i);
> end
>

If your struct exists already, assign in one shot can be done by:

c=num2cell(numbers); [myStruct.electrode(1:10).number]=deal(c{:});
c=num2cell(bankID ); [myStruct.bankID (1:10).number]=deal(c{:});
clear c

% Bruno

% Bruno

Subject: How to assign a vector to a structure?

From: Kian

Date: 15 Jun, 2009 21:04:02

Message: 6 of 8

Thanks again, Bruno! Exactly what I was looking for. I knew brackets were involved somehow.

Subject: How to assign a vector to a structure?

From: Matt

Date: 15 Jun, 2009 21:19:04

Message: 7 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h14r4d$9vh$1@fred.mathworks.com>...

> If your struct exists already, assign in one shot can be done by:
>
> c=num2cell(numbers); [myStruct.electrode(1:10).number]=deal(c{:});
> c=num2cell(bankID ); [myStruct.bankID (1:10).number]=deal(c{:});
> clear c

Bruno- I think this will work even if your struct doesn't already exist...?

Subject: How to assign a vector to a structure?

From: Matt

Date: 15 Jun, 2009 21:21:01

Message: 8 of 8

"Kian " <kian.torab@utah.edu> wrote in message <h16d02$c4o$1@fred.mathworks.com>...
> Thanks again, Bruno! Exactly what I was looking for. I knew brackets were involved somehow.

Bear in mind that all the solutions offered here call M-functions (num2cell, mat2cell, etc...) that use for-loops internally. So you are not really avoiding for loops, only the keystrokes needed to write them yourself.

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