Problem with built in and interp1

조회 수: 2 (최근 30일)
Robert Cumming
Robert Cumming 2012년 11월 21일
All, I'm having an issue that built in cant find the native interp1 function, see code below:
% initialsie
x = [0:0.1:2*pi];
% run a check on syntax and method:
default = sin ( x );
check = builtin ( 'sin', x );
% is it equal? It should be:
isequal ( default, check )
% now I try the same for interp1
default2 = interp1 ( x, check , pi/2 )
% try the same with the builtin command:
check2 = builtin ( 'interp1', x, check, pi/2 );
??? Error using ==> builtin
Cannot find builtin function 'interp1'
Computer details: Windows XP - r2010b
Is this expected behaviour? Is it repeated on other versions/platforms?
I have interp1 overloaded - because the inbuilt interp1 has poor error messages - but I want to be able to run the built in one to verify that the results of the two are the same (automated testing), the only way I can think to do it other than built in is to remove the path to the overloaded one - but it would mean a lot of rmpath, addpath etc.... So I'd like a neater solution...
Thanks

채택된 답변

Jan
Jan 2012년 11월 21일
편집: Jan 2012년 11월 21일
interp1 is not a built-in function, but an M-file. Therefore you observe the expected and wanted behaviour.
See:
which interp1 -all
which sin -all
  댓글 수: 5
Jan
Jan 2012년 11월 21일
편집: Jan 2012년 11월 21일
It is strictly recommended not to overload Matlab's toolbox functions. While overloading them might be useful for the one or other program from the userland, it can be a desaster, when the overloaded function is called from other funcrions of Matlab. Then the different behaviour is a severe cause of errors.
We find corresponding examples in this forum: Somebody overlaoded display() and this function is called from any line, which is not terminated by a semicolon. I had overloaded STRCMP and made a small mistake. Afterwards neither opening the function in the editor, nor shutting down Matlab worked anymore. Killing Matlab and fixing the Matlab path while it is off has been the only chance to start it again.
Therefore the problem of calling the toolbox version of INTERP1 instead of a user-defined one is very easy: Rename the later under all circumstances.
To be sure that such overloading does not happen accidently, e.g. when a new Matlab version contains a function, which was not known when the user created an own function, append folders with user-defined functions at the end of the PATH only. Never, never, never add them before Matlab's own toolbox function. Exception: You are really sure, that you want to replace a function e.g. by a bugfix.
Robert Cumming
Robert Cumming 2012년 11월 21일
Yes I understand (and agree) - however I found that I was getting repeatedly asked why interp1 was crashing for users - so I made a copy and updated the error messages only.
Thanks for your help Jan

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by