Avoid MATLAB startup warning when overloading buil

2019-02-09 21:16发布

问题:

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 ;-) ?

回答1:

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.



回答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


回答3:

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');


回答4:

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.