Path: news.mathworks.com!not-for-mail
From: "us " <us@neurol.unizh.ch>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Operations on Structures
Date: Sun, 29 Jul 2007 02:40:01 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 22
Message-ID: <f8gum1$5lp$1@fred.mathworks.com>
References: <24355223.1185589937727.JavaMail.jakarta@nitrogen.mathforum.org> <1185673994.781506.135630@w3g2000hsg.googlegroups.com>
Reply-To: "us " <us@neurol.unizh.ch>
NNTP-Posting-Host: webapp-01-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1185676801 5817 172.30.248.36 (29 Jul 2007 02:40:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 29 Jul 2007 02:40:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:421562


Alden Griffith:
<SNIP structure field arithmetic...

one of the many solutions

% create some data
     s1.a=1:5;
     s1.b=1:5;
     s2.a=11:15;
     s2.b=101:105;
     fun=@plus; % select your element-by-element function
% the engine
     fn=fieldnames(s1);
     fh=@(sa,sb,fd,fun) fun(sa.(fd),sb.(fd));
     r=cellfun(@(x) fh(s1,s2,x,fun),fn,'uni',false);
     r=cell2struct(r,fn);
% the result
     s1
     s2
     r

us