From mboxrd@z Thu Jan 1 00:00:00 1970 From: Darlan Cavalcante Moreira Subject: Re: Re: org-babel matlab example Date: Tue, 14 Sep 2010 15:23:46 -0300 Message-ID: <4c8fbdb6.0676970a.1c23.ffffb64f@mx.google.com> References: <20100911.101806.1287597470496846955.etimecowboy@gmail.com> <87iq2c2t9t.fsf@stats.ox.ac.uk> <87iq2c2qru.fsf@stats.ox.ac.uk> <20100912.084540.2128491266708771935.etimecowboy@gmail.com> <4c8e1c34.14c2640a.1e85.21cc@mx.google.com> <87r5gwbaf3.fsf@stats.ox.ac.uk> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Return-path: Received: from [140.186.70.92] (port=56739 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OvaAc-0005BX-17 for emacs-orgmode@gnu.org; Tue, 14 Sep 2010 14:23:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OvaAa-00055N-GU for emacs-orgmode@gnu.org; Tue, 14 Sep 2010 14:23:53 -0400 Received: from mail-gx0-f169.google.com ([209.85.161.169]:48450) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OvaAa-00055B-Br for emacs-orgmode@gnu.org; Tue, 14 Sep 2010 14:23:52 -0400 Received: by gxk9 with SMTP id 9so3345617gxk.0 for ; Tue, 14 Sep 2010 11:23:51 -0700 (PDT) In-Reply-To: <87r5gwbaf3.fsf@stats.ox.ac.uk> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Dan Davison Cc: etimecowboy , emacs-orgmode@gnu.org Hello Dan, I wasn't sure if I had suggested this before or not, but now I remember that it was in a thread about python. I have only used org babel with python and octave and I don't know how this would translate to other languages, but I think it makes sense in languages that you can work with scripts. Let me elaborate. Suppose I have the two code snippets below: --8<---------------cut here---------------start------------->8--- #+begin_src python :results value from numpy import arange print "I don't want to see this" a=arange(1,10) a+2 # Only this as the last statement is not enough return a+2 # We need to add return a+2 #+end_src --8<---------------cut here---------------end--------------->8--- --8<---------------cut here---------------start------------->8--- #+begin_src octave :results value disp 'I dont want to see this' a=1:10 b=a+2 # Only this is not enough because ans was never set ans = b #+end_src --8<---------------cut here---------------end--------------->8--- In both cases I want to see the value of a variable after executing a script and not what the script prints to the screen. From babel documentation I can see that I need the "functional mode" with the header argument :results value. However, in the python example I need to add a "return" statement even though I didn't declare a python function in the code (this is invalid code if I tangle it). Also, in the octave example if the 'ans' variable was set before to some value, babel would show it after execution, instead of the value of the last statement as one could think. Therefore, the suggestion is that since babel is implicitly declaring a function (with a def in python, for example), it is more natural that it also "write" the return statement which I could set with a ":return" header argument and not in the language code. The two snippets would become --8<---------------cut here---------------start------------->8--- #+begin_src python :return b from numpy import arange print "I don't want to see this" a=arange(1,10) b=a+2 #+end_src --8<---------------cut here---------------end--------------->8--- --8<---------------cut here---------------start------------->8--- #+begin_src octave :return b disp 'I dont want to see this' a=1:10 b=a+2 #+end_src --8<---------------cut here---------------end--------------->8--- where the ":results" argument would not be necessary to specify "functional mode". -- Darlan At Tue, 14 Sep 2010 16:40:48 +0100, Dan Davison wrote: > > Darlan Cavalcante Moreira writes: > > > Can I leave a suggestion here? > > Maybe a header argument > > ,---- > > | :return variableName > > `---- > > could be interesting in org babel. This would make babel behavior more > > uniform in all supported languages and avoid the necessity of adding code > > "only to make babel work". > > Hi Darlan, > > Thanks (and sorry if you've made this suggestion previously, which I > seem to recall). I can see that this would be particularly natural for > matlab/octave in which (aIui) function definitions require specifying > the name of the variable whose value is to be returned. In other > languages which I've encountered, return values are determined by either > "return" statements, or the value of the last statement in the function, > or both of these mechanisms. So if we were to apply your suggestion to > all languages, one could argue that it would permit un-natural code in > languages other than matlab/octave. As you say, we do want things to be > uniform across languages, but perhaps there is an argument for > restricting your suggestion to matlab/octave? > > Dan > > > > > -- > > Darlan > > > > At Sun, 12 Sep 2010 08:45:40 +0100 (BST), > > etimecowboy wrote: > >> > >> From: Dan Davison > >> Subject: Re: org-babel matlab example > >> Date: Sat, 11 Sep 2010 12:23:17 -0400 > >> > >> > Dan Davison writes: > >> > > >> >> etimecowboy writes: > >> >> > >> >>> Hi everybody, > >> >>> > >> >>> I am learning to use org-babel to evaluate in-line Matlab code block, > >> >>> but I cannot make it works when I press the "C-c C-c", nor "C-c C-v > >> >>> e". > >> >> > >> >> What happens when you try C-c C-c or C-c C-v e ? There is an issue with > >> >> matlab and octave: they return the value of the last /unassigned/ > >> >> computation > >> > > >> > A better way to say this is: they return the value of the special > >> > variable "ans". So whatever you do, at the end of the block you need to > >> > ensure that "ans" has the value that you want returned. > >> > > >> > I've updated the Worg page on Matlab and Octave: > >> > > >> > http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-octave-matlab.php > >> > > >> > Dan > >> > > >> > > >> >>, so simply ending the code block with a variable name "x" is > >> >> not enough. Could you try one of the following schemes please, and let > >> >> us know if that solves it: > >> >> > >> >> #+begin_src matlab :exports results > >> >> n = [1:10]; > >> >> x = 5*n+4; > >> >> ans = x > >> >> #+end_src > >> >> > >> >> #+begin_src matlab :exports results > >> >> n = [1:10]; > >> >> x = 5*n+4; > >> >> x + 0 > >> >> #+end_src > >> >> > >> >> > >> >>> The export of code works, but the results was not there. My in-line code is like this: > >> >> > >> >> There was a little typo in your original block: the keyword is > >> >> ":exports" with an s. > >> >> > >> >> Dan > >> >> > >> >>> > >> >>> #+begin_src matlab :export results > >> >>> n = [1:10]; > >> >>> x = 5*n+4; > >> >>> x > >> >>> #+end_src > >> >>> > >> >>> Is there someone can provide me a working example? Thanks a lot. > >> >>> > >> >>> > >> >>> ----------------------------- > >> >>> > >> >>> etimecowboy@gmail.com > >> >>> > >> >>> _.,----,._ > >> >>> .:' `:. > >> >>> .' `. > >> >>> .' `. > >> >>> : : > >> >>> ` .'`':'`'`/ ' > >> >>> `. \ | / ,' > >> >>> \ \ | / / > >> >>> `\_..,,.._/' > >> >>> {`'-,_`'-} > >> >>> {`'-,_`'-} > >> >>> {`'-,_`'-} > >> >>> `YXXXXY' > >> >>> ~^^~ > >> >>> > >> >>> _______________________________________________ > >> >>> Emacs-orgmode mailing list > >> >>> Please use `Reply All' to send replies to the list. > >> >>> Emacs-orgmode@gnu.org > >> >>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode > >> >> > >> >> _______________________________________________ > >> >> Emacs-orgmode mailing list > >> >> Please use `Reply All' to send replies to the list. > >> >> Emacs-orgmode@gnu.org > >> >> http://lists.gnu.org/mailman/listinfo/emacs-orgmode > >> > >> Dear Dan, > >> > >> I have checked the Worg page you updated and tried your example. It works! The ans variable is the secret! Thanks a lot. I suggest org should provide some examples in its user's guide, not only on the topic of Babel but also Table, Hyperlink, Picture, and etc. > >> > >> ----------------------------- > >> > >> etimecowboy@gmail.com > >> > >> _.,----,._ > >> .:' `:. > >> .' `. > >> .' `. > >> : : > >> ` .'`':'`'`/ ' > >> `. \ | / ,' > >> \ \ | / / > >> `\_..,,.._/' > >> {`'-,_`'-} > >> {`'-,_`'-} > >> {`'-,_`'-} > >> `YXXXXY' > >> ~^^~ > >> > >> _______________________________________________ > >> Emacs-orgmode mailing list > >> Please use `Reply All' to send replies to the list. > >> Emacs-orgmode@gnu.org > >> http://lists.gnu.org/mailman/listinfo/emacs-orgmode