Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Interesting Matlab puzzle

March 31, 2019 20 Comments

Here’s a nice little puzzle that came to me from long-time Matlab veteran Andrew Janke:
Without actually running the following code in Matlab, what do you expect its output to be? ‘Yaba’? ‘Daba’? perhaps ‘Doo!’? or maybe it won’t run at all because of a parsing error?

function test
    try
        if (false) or (true)
            disp('Yaba');
        else
            disp('Daba');
        end
    catch
        disp('Doo!');
    end
end

function test try if (false) or (true) disp('Yaba'); else disp('Daba'); end catch disp('Doo!'); end end

To muddy the waters a bit, do you think that short-circuit evaluation is at work here? or perhaps eager evaluation? or perhaps neither?
Would the results be different if we switched the order of the conditional operands, i.e. (true) or (false) instead of (false) or (true)? if so, how and why?
And does it matter if I used “false” or “10< 9.9” as the “or” conditional?
Are the parentheses around the conditions important? would the results be any different without these parentheses?
In other words, how and why would the results change for the following variants?

        if (false) or (true)     % variant #1
        if (true)  or (false)    % variant #2
        if (true)  or (10< 9.9)  % variant #3
        if  true   or  10< 9.9   % variant #4
        if 10> 9.9 or  10< 9.9   % variant #5

if (false) or (true) % variant #1 if (true) or (false) % variant #2 if (true) or (10< 9.9) % variant #3 if true or 10< 9.9 % variant #4 if 10> 9.9 or 10< 9.9 % variant #5

Please post your thoughts in a comment below (expected results and the reason, for the main code snippet above and its variants), and then run the code. You might be surprised at the results, but not less importantly at the reasons. This deceivingly innocuous code snippet leads to interesting insight on Matlab’s parser.
Full marks will go to the first person who posts the correct results and reasoning/interpretation of the variants above (hint: it’s not as trivial as it might look at first glance).
Addendum April 9, 2019: I have now posted my solution/analysis of this puzzle here.

USA visit

I will be travelling in the US (Boston, New York, Baltimore) in May/June 2019. Please let me know (altmany at gmail) if you would like to schedule a meeting or onsite visit for consulting/training, or perhaps just to explore the possibility of my professional assistance to your Matlab programming needs.

Related posts:

  1. Interesting Matlab puzzle – analysis – Solution and analysis of a simple Matlab puzzle that leads to interesting insight on Matlab's parser. ...
  2. An interesting uitree utility – ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...
  3. A couple of internal Matlab bugs and workarounds – A couple of undocumented Matlab bugs have simple workarounds. ...
  4. Matlab DDE support – Windows DDE is an unsupported and undocumented feature of Matlab, that can be used to improve the work-flow in the Windows environment...
  5. Sliders in Matlab GUI – Professional-looking slider controls can easily be integrated in Matlab GUI. ...
  6. Blurred Matlab figure window – Matlab figure windows can be blurred using a semi-transparent overlaid window - this article explains how...
Pure Matlab Undocumented feature
Print Print
« Previous
Next »
Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (email)
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Matlab callbacks for uifigure JavaScript events

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (393)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (159)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (139)
  • Toolbox (9)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Recent Comments
  • Yair Altman (2 days 5 hours ago): Andrea – sqlite4java is an open-source project (https://code.google.com/archi ve/p/sqlite4java). You can modify its code and/or submit an issue on that project’s...
  • Andrea Carignano (2 days 5 hours ago): Thank you very much! I am using the first method of this article: sqlite4java. I have just a little problem. After every operation on database, I get some red messages in...
  • Wil Koenen (9 days 0 hours ago): The undocumented syntax, e.g. classdef MyClass properties prop@double scalar = 3; end end runs fine in R2019a, but produces a warning in R2019b: Warning: Class 'MyClass' uses an...
  • Jacopo (11 days 2 hours ago): I’m currently using Matlab 2019b. For the application (software) I’m currently developing, I would like to modify the toolstrip of a Simulink window I’m opening...
  • BPoliti (15 days 21 hours ago): Hi, Thank you for the very nice article! It has helped me a lot and works like a charm 🙂 One thing with the modified code is that it is necessary to add an attachment to send an...
  • Yair Altman (24 days 23 hours ago): I typically use structs, due to the easy and efficient way that I can aggregate data from different nodes (as long as the nesting is not too deep). For example, [data.age] or...
  • KE (25 days 0 hours ago): Once you read your XML information into Matlab, what kind of ‘container’ do you find it most useful for handing the data? I have tried nonscalar structure arrays (because...
  • Martin Lechner (38 days 9 hours ago): For html strings you have to replace the special characters with the entity names (e.g. replace '<' by '&lt;' or '>' by '&gt;'). For a description to entity...
  • Arun Joe Joseph (39 days 3 hours ago): Is it possible to use this Matlab CheckboxList along with GUIDE? I tried using this in a GUIDE app, and it seems that I cannot pass GUIDE arguments like handles structure to...
  • Bradley Stiritz (39 days 14 hours ago): It’s great to see that Yair and others are digging into the internals of AppDesigner. It seems to have a lot of promise. At the same time, AppDesigner is still often...
  • Vishal Sharma (40 days 9 hours ago): Great help. thanks. I was struggling with Appdesigner, but this post and the links given for mlapp,solved quite a lot of issues.
  • K Xu (43 days 21 hours ago): Trouble is, when there are “<" in the string, they are not shown in Matlab! Any ideas to fix it?
  • Martin Lechner (44 days 1 hour ago): Hi Kyle, I normally initialize the property with properties graphicToPrint Graphic = ConcreteGraphic.empty end and have no problems with the new syntax.
  • Khaled (44 days 17 hours ago): Hello, I found out how Matlab displays bold hyperlinks. It does so by adding an inline style (style=”font-weight:bold ”) inside the <a> tag. For example:...
  • Reza (47 days 18 hours ago): Thank you Yair! In order to analyze the deformation of a surface, I would like to monitor this surface (facade of a building) every 2 hours, for example. so I get every 2 hours a point...
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top