Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to assign a vector to a structure?
Date: Mon, 15 Jun 2009 06:53:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 34
Message-ID: <h14r4d$9vh$1@fred.mathworks.com>
References: <h141it$d34$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1245048781 10225 172.30.248.35 (15 Jun 2009 06:53:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Jun 2009 06:53:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:547445


"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