Information about Matlab try; ... catch;

View: New views
5 Messages — Rating Filter:   Alert me  

Information about Matlab try; ... catch;

by Michael D Godfrey-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Compatibility problems with: try; ... catch; ... end:

This is beyond the strictest "Matlab compatibility"
rule.  But, it can cause trouble.  There are a number
of differences between Matlab (7.7.0 R2208b) and the
current Octave development system for try; catch;.

First, the Matlab problem:

There are 2 scripts attached.  outer.m is the one to run.
It references inner_try.m.  Lines 5 and 6 of inner_try.m show the
Matlab problem.  If line 6 is used and line 5 commented out
Matlab does not show an error.  It runs as if the line
were correct (or as if GAmouse was defined).  Octave handles
this correctly: an error is raised and caught by outer.m.
(In the original code there was no disp(lasterr) in the
catch; end; so Octave just exited for no apparent reason.
Matlab ran OK.)

The only thing that I can think of to do about this (other
than an error report to the Mathworks...) is to somewhere
document this as Matlab behavior that is not compatible with
Octave.

Next, other differences:

1. In outer.m, Matlab will not allow the printf statement
    after the catch;  (line 19)

2. Even more odd, Matlab will not allow the printf statement at
    line 14 of outer.m.  With that line NOT commented Matlab says:
=======================================================
As we draw things, we will list the corresponding graphics
commands on lines starting with ' >> '

  >> printf
Undefined function or method 'printf' for input arguments of type 'char'.
 >>
========================================================
As a separate comment, this code was in with an interesting package
that does Geometric Algebra (called GABLE).  This package makes lots
of use of classes.  The code itself appears to work correctly in
the current Octave.

Michael

GAn=1;
while GAn==1
 GAtmp = who('GAmouse');
% test with one of the 2 lines below, not both.
 if strcmp(GAtmp,'GAmouse') % & GAmouse  % This version "works" for Octave and Matlab
% if strcmp(GAtmp,'GAmouse') & GAmouse   % This version (with error: GAmouse not defined)
                                         % works with Matlab, but fails (as it should)
                                         % with Octave. By "works" with Matlab I mean
                                         % Matlab ignores the error, which is not correct
                                         % behavior, and the program keeps running.
     fprintf(1,'.');
     waitforbuttonpress;
     return
 end
 GAtmp = who('GAps');
 if strcmp(GAtmp,'GAps')
     GAns=input(GAps,'s');
 else
     GAns=input('>>>> Enter a command or press return to continue: >> ','s');
 end
 if strcmp(GAns,'')
   GAn = 0;
 elseif strcmp(GAns,'GAend')
   error('GAend aborts a script');
 elseif ~isempty(findstr(GAns,'GAblock'))
   disp('Can''t have nested GAblock calls.');
 else
   try
     eval(GAns);
   catch
     disp(lasterr);
     disp('Invalid command.  Try again.');
   end
 end
end


try
clc
disp('Welcome to the GABLE demonstration!');disp(' ');
disp('This script demonstrates some of the basic features of GABLE.');
disp('At any prompt, you may hit return to continue the demo or type');
disp('a Matlab command.');
disp(' ');
disp(' ');inner_try;disp(' ');

disp('As we draw things, we will list the corresponding graphics');
disp('commands on lines starting with '' >> ''');
disp(' ');
disp(' >> printf');
%printf('Hello from outer.');       % This command also confuses Matlab
disp(' >> who');
who

catch ;
% printf('at catch in outer.m  ');  %  Matlab does not allow this
disp(lasterr); end


_______________________________________________
Bug-octave mailing list
Bug-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/bug-octave

Re: Information about Matlab try; ... catch;

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Aug 25, 2009 at 4:28 AM, Michael D
Godfrey<godfrey@...> wrote:

> Compatibility problems with: try; ... catch; ... end:
>
> This is beyond the strictest "Matlab compatibility"
> rule.  But, it can cause trouble.  There are a number
> of differences between Matlab (7.7.0 R2208b) and the
> current Octave development system for try; catch;.
>
> First, the Matlab problem:
>
> There are 2 scripts attached.  outer.m is the one to run.
> It references inner_try.m.  Lines 5 and 6 of inner_try.m show the
> Matlab problem.  If line 6 is used and line 5 commented out
> Matlab does not show an error.  It runs as if the line
> were correct (or as if GAmouse was defined).  Octave handles
> this correctly: an error is raised and caught by outer.m.
> (In the original code there was no disp(lasterr) in the
> catch; end; so Octave just exited for no apparent reason.
> Matlab ran OK.)
>

This is the now famous short-circuiting behavior of element-wise
logical operators. In Matlab, they short-circuit in if clauses, and
not elsewhere. This has been discussed mutliple times and the general
agreement is that it was bad design decision and Octave needs not copy
it. Just use && (which I'd recommend in Matlab as well).

> The only thing that I can think of to do about this (other
> than an error report to the Mathworks...) is to somewhere
> document this as Matlab behavior that is not compatible with
> Octave.
>

I believe it is documented somewhere, but I may be wrong.

> Next, other differences:
>
> 1. In outer.m, Matlab will not allow the printf statement
>   after the catch;  (line 19)
>
> 2. Even more odd, Matlab will not allow the printf statement at
>   line 14 of outer.m.  With that line NOT commented Matlab says:
> =======================================================
> As we draw things, we will list the corresponding graphics
> commands on lines starting with ' >> '
>
>  >> printf
> Undefined function or method 'printf' for input arguments of type 'char'.
>>>

Huh? Probably a bug in Matlab, then.

> ========================================================
> As a separate comment, this code was in with an interesting package
> that does Geometric Algebra (called GABLE).  This package makes lots
> of use of classes.  The code itself appears to work correctly in
> the current Octave.
>
> Michael
>

That's cool :) Though I have but a dim understanding what "Geometric
Algebra" is about.

regards

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

_______________________________________________
Bug-octave mailing list
Bug-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/bug-octave

Re: Information about Matlab try; ... catch;

by dbateman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Jaroslav Hajek-2 wrote:
On Tue, Aug 25, 2009 at 4:28 AM, Michael D
Godfrey<godfrey@isl.stanford.edu> wrote:
> Compatibility problems with: try; ... catch; ... end:
>
> This is beyond the strictest "Matlab compatibility"
> rule.  But, it can cause trouble.  There are a number
> of differences between Matlab (7.7.0 R2208b) and the
> current Octave development system for try; catch;.
>
> First, the Matlab problem:
>
> There are 2 scripts attached.  outer.m is the one to run.
> It references inner_try.m.  Lines 5 and 6 of inner_try.m show the
> Matlab problem.  If line 6 is used and line 5 commented out
> Matlab does not show an error.  It runs as if the line
> were correct (or as if GAmouse was defined).  Octave handles
> this correctly: an error is raised and caught by outer.m.
> (In the original code there was no disp(lasterr) in the
> catch; end; so Octave just exited for no apparent reason.
> Matlab ran OK.)
>

This is the now famous short-circuiting behavior of element-wise
logical operators. In Matlab, they short-circuit in if clauses, and
not elsewhere. This has been discussed mutliple times and the general
agreement is that it was bad design decision and Octave needs not copy
it. Just use && (which I'd recommend in Matlab as well).

> The only thing that I can think of to do about this (other
> than an error report to the Mathworks...) is to somewhere
> document this as Matlab behavior that is not compatible with
> Octave.
>

I believe it is documented somewhere, but I may be wrong.
Please check the FAQ and in particular the section

http://www.gnu.org/software/octave/FAQ.html#MATLAB-compatibility

This has a good explanation why short circuiting & and | are a bad idea..

D.



Information about Matlab try; ... catch;

by John W. Eaton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 24-Aug-2009, Michael D Godfrey wrote:

| Next, other differences:
|
| 1. In outer.m, Matlab will not allow the printf statement
|     after the catch;  (line 19)
|
| 2. Even more odd, Matlab will not allow the printf statement at
|     line 14 of outer.m.  With that line NOT commented Matlab says:

Matlab does not have printf.  It provides

  fprintf (FID, FMT, ...)
  fprintf (FMT, ...)

and Octave is compatible with this, so just use fprintf and it should
work in both.

jwe
_______________________________________________
Bug-octave mailing list
Bug-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/bug-octave

Re: Information about Matlab try; ... catch;

by Michael D Godfrey-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 08/25/2009 08:30 AM, John W. Eaton wrote:
> Matlab does not have printf.

Oops!  I forgot that.  Thanks.
One less "bug" for Matlab.

Michael
_______________________________________________
Bug-octave mailing list
Bug-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/bug-octave