6

As described here, I created my own figure.m which nicely overloads the built-in figure command. Now, whenever I start MATLAB I get the warning

Warning: Function C:\somepath\figure.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.

Is there any way to deactivate this warning, given that it is desired behavior in my case?

You might say that I should call my function differently instead of overloading, but I do feel for my development system this overloading is the right way to go...

Update

As mentioned by Aabaz you can globally turn off this warning using

warning off MATLAB:dispatcher:nameConflict

which needs to go at the beginning of matlabrc.m (before the path is set). However, I would still be interested in a solution which could specificially remove this error message for overloading figure.m (or some self-defined list of functions) instead of for all functions. I guess I'm asking a bit too much here ;-) ?

Community
  • 1
  • 1
Jonas Heidelberg
  • 4,984
  • 1
  • 27
  • 41
  • 1
    Alternatively you can hide your implementation of `figure` in `C:\somepath\private`, which makes it visible to the functions in `somepath` only. Thus, you avoid the name conflict and potentially unexpected behavior when you work on a different project that is on `someotherpath`. – Jonas Jun 20 '11 at 12:13
  • @Jonas interesting, I'll give that a try! I would have to add it to a handfull of different paths, but that might indeed be a good option for me. – Jonas Heidelberg Jun 20 '11 at 12:24
  • MATLAB should have probably better designed the we organize path/scope and implemented some sort of namespaces or modules (similar to what other languages offers)... That said, MATLAB does have a "package" functionality, but it seems to have been added as an afterthought, to overcome the problem of namespace collisions. http://www.mathworks.com/help/techdoc/matlab_oop/brfynt_-1.html Other mechanisms to control scope exist such as: private functions, nested and sub-functions. Check out this related question: http://stackoverflow.com/q/2748302 – Amro Jun 20 '11 at 17:37

4 Answers4

4

I cannot seem to replicate this warning with my Matlab version (R2008b) but anyway If you did not already try it you should look into the functions lastwarn and warning that allow you to identify and turn off this warning.

PS: the warning eventually came for some reason and I was able to use lastwarn and warning to turn it off.

>>[msgstr msgid]=lastwarn;
>>disp(msgid);
MATLAB:dispatcher:nameConflict
>>warning('off',msgid);

I should add that you should turn it off at startup for this to be effective between different sessions of Matlab.

Aabaz
  • 3,106
  • 2
  • 21
  • 26
  • Using lastwarn is a good idea, didn't think of that. However since the warning is generated early during startup (when the directory containing my `figure.m` is added to the path), adding warning off to startup.m was not enough - I needed to place it at the beginning of the global matlabrc.m before the MATLAB path is set. – Jonas Heidelberg Jun 20 '11 at 10:04
  • Yes you are right, my Matlab installation does not show this warning every time which is strange, but as you said you could do it in **matalbrc.m**. For your specific problem I guess that you could overload the `warning` function in the same way you overloaded the `figure` function in order to specifically turn off this warning for a set of given functions. – Aabaz Jun 20 '11 at 11:39
  • Since `msgstr` contains the name of the function that is being overloaded, overloading `warning` could in principle work. However, I would be very surprised if MATLAB would actually *call* the overloaded `warning.m` without issuing a warning about overloading it first... a kind of chicken-and-egg problem. Of course you could solve that with a `warning off MATLAB:dispatcher:nameConflict; setmypath; warning on MATLAB:dispatcher:nameConflict` trick, but that starts to get rather dirty ;-). – Jonas Heidelberg Jun 20 '11 at 11:52
2

I just ran into this problem on MATLAB R2014b where I also wanted to override figure. I think this is the closest solution to your updated question (3.5 years later...).

I think using the "dirty" trick from your comment is actually the cleanest, if done smartly as it doesn't require you to change matlabrc.m and can suppress the warning for only functions that you want to override built-in ones.

  1. Put all your default overrides in a folder that is not on your permanent MATLAB path. I keep mine in ~/Documents/MATLAB/overrides on my Mac. I have e.g. ~/Documents/MATLAB/overrides/figure.m
  2. Use startup.m to add overrides to your path with the warning turned off, and then turn it back on:
    warning off MATLAB:dispatcher:nameConflict
    addpath('/Users/victor/Documents/MATLAB/overrides');
    warning on MATLAB:dispatcher:nameConflict

Not sure if tilde expansion works with addpath so I write the full path out.

Doing it this way suppresses the warning for me selectively only for the stuff that gets loaded from overrides. You can, of course, be even more selective with your folder naming. It also means I don't have to change anything in my MATLAB system files so it's localized to my user account and persistent across upgrades (for good or bad; monkey patch responsibly).

To access the built-in figure from my override, I have to cd there temporarily (as otherwise the override will simply call it self). So figure.m would look like this:

function fig = figure(varargin)

% Call original figure function
old = pwd;
cd(fullfile(matlabroot, 'toolbox', 'matlab', 'graphics', ''));
fig = figure(varargin{:});
cd(old);

% ...
% Do dirty override magic

end
vicvicvic
  • 6,025
  • 4
  • 38
  • 55
1

I can't comment yet, so I'll just expand the answer given by vicvicvic further here. The general process stays the same, however it has some further fine tunings.

  1. Put your override-function figure.m in a folder which is not on your current MATLAB path, e.g. /users/heidelberg/.matlab/_overload. For me, tilde expansion is supported, but I would not rely on it. However, you could also put it in a subfolder of a MATLAB startup script (see below).
  2. Use startup.m to add your override folder to the path. To avoid the warning, make sure it is turned off, and then restore its original state

    % save the current state while switching it off
    warningState = warning('off', 'MATLAB:dispatcher:nameConflict'); 
    
    addpath('/users/heidelberg/.matlab/_overload');
    
    % restore the saved state
    warning(warningState);
    
    % cleanup
    clear('warningState');
    

    The difference here is that if e.g. your administrator set the warning to be off anyway, you won't accidentally switch it back on.

  3. In your implementation of figure, at some point you will probably have to call the builtin version. vicvicvic suggested a cd to the directory, however there also is the MATLAB function builtin, which does that job for you:

    function fig = figure(varargin)
    % overload function
    
    % call builtin figure
    varargout = cell(1, nargout);
    [varargout{:}] = builtin('figure', varargin{:});
    
    % do you magic here
    % ...
    
    end
    

    Also, use varargout and nargout to preserve for an arbitrary number of output arguments (might be irrelevant here and now, but for other functions or future releases it might be important).


Annotation

A method I prefer is to have a subfolder in the directory where my startup.m file is stored, called e.g. _overload. For me this is /users/timm/Documents/MATLAB/_overload. To easily add this folder, use the following script:

File /users/timm/Documents/MATLAB/startup.m

    % extract the current directory (pwd can fail if started elsewhere)
    [currentPath, ~, ~] = fileparts(mfilename('fullpath'));

    % add the path, compare above
    warningState = warning('off', 'MATLAB:dispatcher:nameConflict');
    addpath([currentPath, filesep(), '_overload']);
    warning(warningState);

    % cleanup
    clear('currentPath', 'warningState');
Timm
  • 202
  • 1
  • 11
1

Adding a directory that contains the function overload to the search path will display the warning whenever a function in that directory is edited and saved, no matter if the directory is added in startup.m or not.

A simple way to solve this is to put overloading functions in a package. Then import the package in startup. No need to mess with warnings.

MrcJkb
  • 85
  • 1
  • 8