m files as Function Inputs !!

Dear all, I have several files with real data representing signals and in each file I draw the kernel estimate function for the data. I have another m file to calculate the delay so I want the two parameters for the delay function to be any signal from the files I created so when I runi it using the command I can choose any two signals and It will calculate the delay.. Not sure how to pass the two files as arguments.How can I in the xcorr argument say two different data?
function d = delayMethod(filename1,filename2)
data = importdata(filename);
[Rxx, lags] = xcorr(data, data);
[Y, delay] = max(Rxx);
lags(d)
end
Thanks, Susan

2 Comments

Is your file a .m file or .mat file? Your title says .m file. Answers below assume .mat file.
If your file is .m file, then the variable name 'data' is dependent on the code inside the .m file.
It is m file !

Sign in to comment.

 Accepted Answer

function d = delayMethod(filename1,filename2)
eval(filename1);
eval(filename2);
%data1 and data2 shall be whatever the related variable name
[Rxx, lags] = xcorr(data1, data2);
[Y, delay] = max(Rxx);
lags(d)
call it through
delayMethod('work1','work2');
I am not sure what lags(d) means though!

8 Comments

Error in ==> delayMethod at 2
eval(filename1); Undefined function or variable "work1.m"
I have m files corresponding to work1 and work2 ??
and its lags(delay)
Sorry, should be delayMethod('work1','work2') without the '.m'!
It looks like the return argument "d" of your function delayMethod() is not assigned.
I can't understand your comments above. Read it yourself. It's really broken!
Sorry, I meant it works now but It displays a figure though and I am not sure where in my code I programmed to display a figure? I am comparing two signals from two file but the most Important how Can I make it work for any signals without having to modify the variables in the xcorr arguments?
I tried different things to figure out which figure is plotting and it actually plots the first file I put in the argument !! Do you know how to stop it from doing this ? I just need the delay value !
I have no idea what is inside your work1.m. You'll have to do whatever is necessary to not to make the plot. Regarding the changing variable names, that is something you have to deal with based on your programming structure. The best I can suggest is to make your work1.m also a function with return argument so you can do like data1=work1;data2=work2; But then you'll have to modify your delayMethod() too. Anyway, there are ways to deal with it. You just need to plan ahead and deal with it one at a time.
but I want it to work for all the data files, so I will just have to type the files I want to calculate the delay for but pre-specifying it in the code data1 = work1 etc is not feasible solution for me !!

Sign in to comment.

More Answers (3)

d = delayMethod('file1.mat','file2.mat');
calling something that uses the inputs:
function d = delayMethod(filename1,filename2)
data1 = importdata(filename1);
data2 = importdata(filename2);
[Rxx, lags] = xcorr(data1, data2);
[Y, delay] = max(Rxx);
lags(d)
end

1 Comment

I run it in the command using your above line and I got an error,
[Rxx, lags] = xcorr(data1, data2);
didn't recognize the input arguments data1 and data2???

Sign in to comment.

function d = delayMethod(filename1,filename2)
data1 = importdata(filename1);
data2 = importdata(filename2);
[Rxx, lags] = xcorr(data1, data2);
[Y, delay] = max(Rxx);
lags(d)
end

12 Comments

I like your naming convention!
How do you run it from the command, I did delaymethod(work1,work2) and I got this error Too many output arguments, even If I make it as m file still got the same error, delaymethod(work1.m,work2.m)??
I make it string actually but It produced another error
[Rxx, lags] = xcorr(data1, data2) Input arguments not recognized dat1,data2
What do you get if you run "clear all;work1"?
What do you get if you run "clear all;work2"?
It basically run it and I get all the variables in the variable workspace
I get the data drawn in the axes and the kernel estimate Curve !!
All right! After you run work1.m, which variable are you going to use as the first input argument for delayMethod()? And which variable in work2.m are you going to use for the second argument? Is the variable name always the same for work3.m, work4.m ..., or is it different for every .m file?
In each file I used different names in some and others just X !! Do I need to make all use different names?
@Sean de
I posted my answer about the same time as you and was funny to see it already there :)
I created one Function for all the signals I have because the code pretty much is the same !
Yeuk! You need to do it differently! It is poor programming but just get you through!
Paulo, I know!
I posted mine, and when it returned yours was there, with identical recommendations. We've been brainwashed by the forum!

Sign in to comment.

Susan
Susan on 19 Aug 2011

0 votes

I am still stuck with this.. I tried different things to check what is the function doing,I found out that its plotting the first file I put in the argument and I don't want it to do this, I am just interested in the delay variable. The other thing, I made all the variables names in all the file X so in the xcorr(X,X) thats what I did I run work2,work3 and both of their variables are X so in the xcorr(X,X), I got the delay 83 and If I swap it I get the delay 90 (by swap it I mean work3,work2).. I also, changed the variables name so I made work2 Y and work 3 X so in xcorr(Y,X) I got 83 and if i swap (work3,work2) I still get 83?? I don't understand why in the above case I get 90??

1 Comment

You really really need to go back to the basics of MATLAB. You don't have a clue of what you are doing after so many questions.
If you name the variable the same as "X" for work2.m and work3.m, then when you run delayMethod('work2','work3'), it is going to run work2 first, then run work3, the X variable is over-written. xcorr(X,X) is calculate the correlation of X itself from work3. If you switch the order, it is going to calculate the correlation of X itself from work2.
If you name the variable Y for work2 and X for work3, xcorr(Y,X) will be the same even if your switch the order delayMethod('work2','work3'), or delayMethod('work3','work2').

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!