<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674</link>
    <title>MATLAB Central Newsreader - How to assign a vector to a structure?</title>
    <description>Feed for thread: How to assign a vector to a structure?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 14 Jun 2009 23:37:01 -0400</pubDate>
      <title>How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657234</link>
      <author>Kian </author>
      <description>I have a structure and an array of values that I'd like to assign to that structure. Example:&lt;br&gt;
&lt;br&gt;
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 &amp; 2 for each 5 electrodes.&lt;br&gt;
&lt;br&gt;
numbers = 1:10;&lt;br&gt;
bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];&lt;br&gt;
&lt;br&gt;
How do I assign these two arrays to these structures without a for-loop?&lt;br&gt;
myStruct.electrode.number&lt;br&gt;
myStruct.electrode.bankID&lt;br&gt;
&lt;br&gt;
I'd think a command like this would do the job, but it doesn't:&lt;br&gt;
&lt;br&gt;
myStruct.electrode(1:10).number = numbers;&lt;br&gt;
myStruct.electrode(1:10).bankID = bankID;&lt;br&gt;
&lt;br&gt;
So instead, I use:&lt;br&gt;
&lt;br&gt;
for i = 1:10&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;myStruct.electrode(i).number = numbers(i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;myStruct.electrode(i).bankID = bankID(i);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Thanks in advance!&lt;br&gt;
&amp;nbsp;&amp;nbsp;</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 00:22:01 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657241</link>
      <author>Sadik </author>
      <description>Hi Kian,&lt;br&gt;
&lt;br&gt;
I tried to make it one line:&lt;br&gt;
&lt;br&gt;
s = cell2struct([mat2cell((1:10)',10,1) mat2cell([ones(5,1);2*ones(5,1)],10,1)],{'number','bankID'},2);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Kian &quot; &amp;lt;kian.torab@utah.edu&amp;gt; wrote in message &amp;lt;h141it$d34$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I have a structure and an array of values that I'd like to assign to that structure. Example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 &amp; 2 for each 5 electrodes.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; numbers = 1:10;&lt;br&gt;
&amp;gt; bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How do I assign these two arrays to these structures without a for-loop?&lt;br&gt;
&amp;gt; myStruct.electrode.number&lt;br&gt;
&amp;gt; myStruct.electrode.bankID&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'd think a command like this would do the job, but it doesn't:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; myStruct.electrode(1:10).number = numbers;&lt;br&gt;
&amp;gt; myStruct.electrode(1:10).bankID = bankID;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So instead, I use:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 1:10&lt;br&gt;
&amp;gt;    myStruct.electrode(i).number = numbers(i);&lt;br&gt;
&amp;gt;    myStruct.electrode(i).bankID = bankID(i);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks in advance!&lt;br&gt;
&amp;gt;   </description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 03:56:01 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657274</link>
      <author>Kian </author>
      <description>Sadik,&lt;br&gt;
&lt;br&gt;
Thanks for your response, but that doesn't achieve what I wanted to do. What you did is done by:&lt;br&gt;
&lt;br&gt;
s.number = numbers';&lt;br&gt;
s.bankID = bankID';&lt;br&gt;
&lt;br&gt;
I want the following:&lt;br&gt;
&lt;br&gt;
s(1).number = 1&lt;br&gt;
s(3).bankID = 1&lt;br&gt;
s(7).bankID = 2&lt;br&gt;
and so on...&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
Kian</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 04:20:17 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657279</link>
      <author>Sadik </author>
      <description>You are right, sorry. This one should work:&lt;br&gt;
&lt;br&gt;
s = struct('number', mat2cell((1:10)',ones(10,1),1), 'bankID', mat2cell([ones(5,1);2*ones(5,1)],ones(10,1),1));&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Kian &quot; &amp;lt;kian.torab@utah.edu&amp;gt; wrote in message &amp;lt;h14goh$6lc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Sadik,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your response, but that doesn't achieve what I wanted to do. What you did is done by:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s.number = numbers';&lt;br&gt;
&amp;gt; s.bankID = bankID';&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s(1).number = 1&lt;br&gt;
&amp;gt; s(3).bankID = 1&lt;br&gt;
&amp;gt; s(7).bankID = 2&lt;br&gt;
&amp;gt; and so on...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&amp;gt; Kian</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 06:53:01 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657293</link>
      <author>Bruno Luong</author>
      <description>&quot;Kian &quot; &amp;lt;kian.torab@utah.edu&amp;gt; wrote in message &amp;lt;h141it$d34$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I have a structure and an array of values that I'd like to assign to that structure. Example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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 &amp; 2 for each 5 electrodes.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; numbers = 1:10;&lt;br&gt;
&amp;gt; bankID = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How do I assign these two arrays to these structures without a for-loop?&lt;br&gt;
&amp;gt; myStruct.electrode.number&lt;br&gt;
&amp;gt; myStruct.electrode.bankID&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'd think a command like this would do the job, but it doesn't:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; myStruct.electrode(1:10).number = numbers;&lt;br&gt;
&amp;gt; myStruct.electrode(1:10).bankID = bankID;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So instead, I use:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 1:10&lt;br&gt;
&amp;gt;    myStruct.electrode(i).number = numbers(i);&lt;br&gt;
&amp;gt;    myStruct.electrode(i).bankID = bankID(i);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
If your struct exists already, assign in one shot can be done by:&lt;br&gt;
&lt;br&gt;
c=num2cell(numbers); [myStruct.electrode(1:10).number]=deal(c{:});&lt;br&gt;
c=num2cell(bankID ); [myStruct.bankID (1:10).number]=deal(c{:});&lt;br&gt;
clear c&lt;br&gt;
&lt;br&gt;
% Bruno&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 21:04:02 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657525</link>
      <author>Kian </author>
      <description>Thanks again, Bruno! Exactly what I was looking for. I knew brackets were involved somehow.</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 21:19:04 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657533</link>
      <author>Matt </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h14r4d$9vh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; If your struct exists already, assign in one shot can be done by:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; c=num2cell(numbers); [myStruct.electrode(1:10).number]=deal(c{:});&lt;br&gt;
&amp;gt; c=num2cell(bankID ); [myStruct.bankID (1:10).number]=deal(c{:});&lt;br&gt;
&amp;gt; clear c&lt;br&gt;
&lt;br&gt;
Bruno- I think this will work even if your struct doesn't already exist...?</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 21:21:01 -0400</pubDate>
      <title>Re: How to assign a vector to a structure?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253674#657536</link>
      <author>Matt </author>
      <description>&quot;Kian &quot; &amp;lt;kian.torab@utah.edu&amp;gt; wrote in message &amp;lt;h16d02$c4o$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thanks again, Bruno! Exactly what I was looking for. I knew brackets were involved somehow.&lt;br&gt;
&lt;br&gt;
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. </description>
    </item>
  </channel>
</rss>

