1

I recently found out that you can get Octave to warn when you are using features not compatible with Matlab. Due to working with others this feature is appealing.

warning ('on', 'Octave:matlab-incompatible')

However when I use it in even simple scripts

warning ('on', 'Octave:matlab-incompatible');
x = 5;
plot(x);

I get many warning due to the implementation of plot using non-Matlab compatible features. For example

warning: potential Matlab compatibility problem: ! used as operator near line 215 offile /usr/share/octave/3.8.1/m/plot/draw/plot.m

Is there a way to turn off these warnings? I don't care if plot is implemented using non-Matlab features because when I use Matlab its implementation will be fine.

horchler
  • 18,384
  • 4
  • 37
  • 73
Andy T
  • 337
  • 1
  • 2
  • 14
  • The [online documentation for Octave's `warning`](https://www.gnu.org/software/octave/doc/interpreter/Enabling-and-Disabling-Warnings.html#Enabling-and-Disabling-Warnings) is poor, but if it's similar to [Matlab's `warning`](http://www.mathworks.com/help/matlab/ref/warning.html) you should be able to selectively disable warnings. I can't test this as I don't have Octave now. If you solve this, feel free to write up (and even accept) your answer. – horchler Dec 11 '14 at 15:41
  • Hi, yes I read the docs and nothing jumped out as a way to progress. I know I can selectively disable warnings. I might need something like only apply this selectively disabled warning to this file and not functions it calls... – Andy T Dec 11 '14 at 15:44
  • Yes, warning are global. The standard way to do what you want is to turn off the offending warnings before calling the function that might trigger them and re-enable them afterward. No elegant, but that's how it works. – horchler Dec 11 '14 at 15:49
  • That's a shame because no one is going to do that every time they call a standard function; such as plot. It sort of makes "warning ('on', 'Octave:matlab-incompatible')" a redundant feature. – Andy T Dec 11 '14 at 15:52

2 Answers2

2

No that is not possible which makes the Octave:matlab-incompatible almost useless. Also, that warning is only printed for syntax so you can still use Octave only functions (such as center or sumsq) without any problem.

I recommend you use a text editor that has separate Matlab and Octave syntax highlight (such as gedit) and avoid things that don't get highlighted.

carandraug
  • 12,938
  • 1
  • 26
  • 38
0

Here's how it's done in Matlab, which looks to be similar to the Octave case:

warning('on', 'Octave:matlab-incompatible'); % Your Octave warning
x = 5;
WarnState = warning('off', 'Offending_MSGID'); % You'll need to get the specific ID
plot(x);
warning(WarnState); % Restore

Yes, it's a bit clumsy. There's no way to specify that a warning is not enabled within a particular file that I know of.

One thing that can happen is that the code an be interrupted by the user or an error before the warning state is restored. In this case, Your system now is in an unknown warning state. One way to avoid this is to use onCleanup. This function is called when a function exits, even if it exits due to an error. You might rewrite the above as:

warning('on', 'Octave:matlab-incompatible'); % Your Octave warning
x = 5;
WarnState = warning('off', 'Offending_MSGID'); % You'll need to get the specific ID
C = onCleanup(@()warning(WarnState));
plot(x);
...

Note that onCleanup won't be called until the function exits so the warning state won't be restored until then. You should be able to add a warning(WarnState); line to manually restore before if you want. Just be sure that whatever function onCleanup is calling can never return an error itself.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Hi, yes it is a solution but very messy. For instance I'm sure lots of core functions are implemented in non-matlab compatible ways so one might have to turn warnings on and off regularly. Its a deal breaker for me... – Andy T Dec 11 '14 at 16:05
  • @AndyT: My only other suggestion is to turn something like this into a function so it's just one line. – horchler Dec 11 '14 at 16:09