Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: struct assignment
Replies: 2   Last Post: Sep 15, 2000 12:27 PM

 Messages: [ Previous | Next ]
 Bruno Haider Posts: 2 Registered: 12/7/04
struct assignment
Posted: Sep 14, 2000 10:46 AM

--------------D5C71F422A23B7D49D7B27C3
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,
I have an array of doubles which I want to assign to a nested struct. I
suppose any struct assignment could be done with some ugly FOR loop but
there should be something more efficient. I thought something with
"num2cell" and "cell2struct" might work but I could not figure out how.

The struct is defined as

segment = struct('slope', 0, 'brkpt', 0)
segments(1:2) = segment
zone = struct('Offset', 0, 'segments', segments, 'pad', 0 )
zones(1:4) = zone

"zones" is a struct array of length 4 with fields 'Offset', 'segments'
and 'pad'; the field 'segments' is again a struct array of length 2 with
fields 'slope' and 'brkpt'

The data array is

data = [ 1 7 13 19 % row assigned to 'Offset'
2 8 14 20 % row assigned to 'slope(1)'
3 9 15 21 % row assigned to 'brkpt(1)'
4 10 16 22 % row assigned to 'slope(2)'
5 11 17 23 % row assigned to 'brkpt(2)'
6 12 18 24 ] % row assigned to 'pad'

Any help would be greatly appreciated.
Bruno Haider

--------------D5C71F422A23B7D49D7B27C3
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi,
<br>I have an array of doubles which I want to assign to a nested struct.
I suppose any struct assignment could be done with some ugly FOR loop but
there should be something more efficient. I thought something with "num2cell"
and "cell2struct" might work but I could not figure out how.
<p>The struct is defined as
<p><tt>segment = struct('slope', 0, 'brkpt', 0)</tt>
<br><tt>segments(1:2) = segment</tt>
<br><tt>zone = struct('Offset', 0, 'segments', segments, 'pad', 0 )</tt>
<br><tt>zones(1:4) = zone</tt><tt></tt>
<p>&nbsp;"zones" is a struct array of length 4 with fields 'Offset', 'segments'
and 'pad'; the field 'segments' is again a struct array of length 2 with
fields 'slope' and 'brkpt'
<p>The data array is<tt></tt>
<p><tt>data = [ 1&nbsp;&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp; 13&nbsp;&nbsp;&nbsp;
19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; % row assigned to 'Offset'</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;
8&nbsp;&nbsp;&nbsp; 14&nbsp;&nbsp;&nbsp; 20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
% row assigned to 'slope(1)'</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;
9&nbsp;&nbsp;&nbsp; 15&nbsp;&nbsp;&nbsp; 21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
% row assigned to 'brkpt(1)'</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;
10&nbsp;&nbsp;&nbsp; 16&nbsp;&nbsp;&nbsp; 22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
% row assigned to 'slope(2)'</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;
11&nbsp;&nbsp;&nbsp; 17&nbsp;&nbsp;&nbsp; 23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
% row assigned to 'brkpt(2)'</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp;
12&nbsp;&nbsp;&nbsp; 18&nbsp;&nbsp;&nbsp; 24 ]&nbsp;&nbsp;&nbsp;&nbsp;
<br><tt></tt>&nbsp;<tt></tt>
<p><tt>Any help would be greatly appreciated.</tt>
<br><tt>Bruno Haider</tt>
<br><tt></tt>&nbsp;</html>

--------------D5C71F422A23B7D49D7B27C3--

Date Subject Author
9/14/00 Bruno Haider
9/15/00 E.Pasterkamp
9/15/00 Bruno Haider