Interp1 error when I moved the .m files into a different folder

Hello guys,
I got an interp1 error when I moved the .m files into a different folder at C:\Research\Interpolation. It still works well in the original folder (at C:\Users\Owner). What happened to that? There was no more message other than just "error" in Matlab.
Here is the script.
clear;
A=importdata('1.txt');
x = A(:,1);
v = A(:,2);
xq = 0:0.001:0.4;
figure
subplot(1,1,1)
vq1 = interp1(x,v,xq);
%plot(x,v,'o',xq,vq1,'-', 'Linewidth',3);
plot(x,v,'o','Linewidth',1); hold on;
plot(xq,vq1,'-', 'Linewidth',2);
%subplot(1,2,2)
%plot(xq,vq1,':.');
I would really appreciate it if you advise.
Thank you very much, Jin

7 个评论

"...I moved the .m files..."
What mfiles did you move? Did you move interp1 ?
Please show us the entire error message. This means all of the red text.
There was no more message other than just "error" in Matlab
Really? If you got only "error", how do you know that it concerns interp1? What exactly are "the .m files", which are moved?
Use at least the debugger to find the failing line:
dbstop if error
Then run the code again. What do you observe?
Thank you so much Stephen and Jan for your quick responses,
I copied "interp1.m" and "1.txt" in some folder. Here is the red message. Sorry for the Korean, which I translated:
SCRIPT interp1을(를) 함수로 실행하려고 했습니다. (means "tried to run SCRIPT interp1) C:\Research\Interpolation\interp1.m
오류 발생(means error occurs): interp1 (line 15) vq = interp1(x,v,xq);
Something wrong with the use of interp1, although this works in the original folder?
"I copied "interp1.m" and "1.txt" in some folder."
"Something wrong with the use of interp1, although this works in the original folder?"
Nothing is wrong with interp1. But it will not work in any other folder other than the original one. That is how many/most MATLAB functions are: they cannot simply be copied and used anywhere that you want. Who told you that they can be?
Note that this also applies to your own code, once it is complicated enough to have local/private/nested/... functions then you will also realize that it makes no sense for any function to work wherever you happen to have put it. And then there is the MATLAB Search Path on top of that... and the license conditions...
Whoever told you that you can copy and use MATLAB functions anywhere was incorrect.
Thank your for the kind explanation.
I am sorry, I do not understand yet. I have done the "copy" work several time, although I have not used the "interp1" command at that time. Do you mean I am not able to copy the file or command?
Assume that I made a mfile for a case. If I want to creat other mfiles with the same functions for different problems, what can I do?
Also, I made a new mfile and a script using interp1, which did not work. I cannot believe it, but is it true that the function "interp1" only applies to 1 file on a computer? When I used "interp1" in the new script, I did not copy the function but typed in person.
"is it true that the function "interp1" only applies to 1 file on a computer"
I am not sure what you mean by "applies". However interp1 can be called as many times you like, from as many scripts or functions as you wish... there is no limit. You have to ensure that those scripts/functions are on the MATLAB Search Path or the Current Directory.
If you are simply calling the interp1 function from a script/function in the current directory then interp1 will work. It is not totally clear what you have attempted so far.
You should not copy the code of interp1 anywhere: this will not work.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-7-4
编辑:Jan 2017-7-4
The file interp1.m is located inside C:\Program Files\MATLAB\<MatlabVersion>\toolbox\matlab\polyfun\ . It must be stored there, because it uses subfunctions stored there and in the \private subfolder. interp1 will not run, if you copy it anwhere else. And even if it would run: There is no need to copy it. Leave it there. interp1 belongs to the Matlab installation and the "polyfun" folder is contained in Matlab's path, such that it is found by Matlab, if it is there.
Creating copies of your own files works reliably, as long as there are not multiple files with the same name inside Matlab's path.

类别

在 帮助中心 和 File Exchange 中查找有关 대용량 파일과 빅 데이터 的更多信息

标签

编辑:

Jan
2017-7-4

Community Treasure Hunt

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

Start Hunting!