Path: news.mathworks.com!not-for-mail From: "Steven_Lord" <slord@mathworks.com> Newsgroups: comp.soft-sys.matlab Subject: Re: coping with "no indexing of function calls" Date: Thu, 30 May 2013 13:55:27 -0400 Organization: MathWorks Lines: 80 Message-ID: <ko83qf$4fo$1@newscl01ah.mathworks.com> References: <ko7tn0$5n$1@reader1.panix.com> NNTP-Posting-Host: ah-slord.dhcp.mathworks.com Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Trace: newscl01ah.mathworks.com 1369936527 4600 172.28.9.169 (30 May 2013 17:55:27 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Thu, 30 May 2013 17:55:27 +0000 (UTC) In-Reply-To: <ko7tn0$5n$1@reader1.panix.com> X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 14.0.8089.726 X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8089.726 Xref: news.mathworks.com comp.soft-sys.matlab:796429 "kj" <no.email@please.post> wrote in message news:ko7tn0$5n$1@reader1.panix.com... > > I'm very new to MATLAB, but AFAIK, the following syntax is > categorically disallowed: > > A = foo(B)(1); > > Here foo is some function. Ditto if one replaces (1) with any > other indexing subexpression (e.g. {42:end, 'frobozz'}, etc.). Correct, it is disallowed. In languages that only allow functions to return one output argument, that type of syntax may make sense. But functions in MATLAB can return multiple outputs; if that syntax were allowed, what would you expect this to return? [maxValues, maxLocations] = max(magic(5))(3) Should it return the third element of each of the regular outputs from MAX? Should it only apply to maxValues and return all of maxLocations from the MAX call? What about cases where the function output arguments are different sizes? [R, p] = chol(magic(6))(:, 2) If you execute: [R, p] = chol(magic(6)) In this case, R should be a 2-by-2 matrix and p should be the 1-by-1 scalar 3. [The second output, p, is ALWAYS a scalar.] So asking for the second column of R makes sense, but asking for the second column of the scalar p does not and would error. We could probably invent some syntax to allow different indexing into different outputs of the function, but it would probably be difficult to read due to its generality. > This means that my code is riddled with lines like > > A = foo(B); > A = A(1); > > X = bar(A); > X = X{42:end, 'frobozz'}; > > I find this makes the code harder to read (though, I admit, this > is extremely subjective, and probably the result of three decades > of reading and writing code in languages that don't require such > circumlocutions). > > Is there some other way to cope with this restriction? I have a couple suggestions. If the multiline code is causing the readability problem, you can put multiple commands on one line. A = foo(B); A = A(1); You could write a function that accepts the temporary output from the function and the indices and extract just those elements of the temporary output. extractCertainElementsFromOneOutputFunction = @(tmp, index) tmp(index); A = extractCertainElementsFromOneOutputFunction(foo(B), 1) You could modify foo and bar to accept inputs that indicate what elements of the output should be returned and return just those outputs, somewhat like SIZE does. M = ones(4, 5, 6); sz = size(M) % size in all the non-trailing-singleton dimensions sz2_twostep = sz(2) sz2_onestep = size(M, 2) % size in the 2nd dimension only -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com