Path: news.mathworks.com!not-for-mail
From: "Sadik " <sadik.hava@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to assign a vector to a structure?
Date: Mon, 15 Jun 2009 00:22:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <h14479$o0f$1@fred.mathworks.com>
References: <h141it$d34$1@fred.mathworks.com>
Reply-To: "Sadik " <sadik.hava@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1245025321 24591 172.30.248.37 (15 Jun 2009 00:22:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Jun 2009 00:22:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1666517
Xref: news.mathworks.com comp.soft-sys.matlab:547390

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