Avoid MATLAB startup warning when overloading buildin functions?

此生再无相见时 提交于 2019-11-30 14:07:38

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!