Macro help writing fucntion in matlab
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi there,
I know that this is such a simple question but I have read and reread the Q&A section etc and I cant find any help. I really think my program should work with ease but it keep throwing up the same error! Ok, so I am trying to write a macro so that i can simply leave my program running over night. I wrote a very similar macro however, it is not working with this program at all. Here is my code;
out = {'fft1' 'fft2'};
for k = 1:2
matFilename = sprintf('Crun%d.txt', k);
matData = load(matFilename);
y2 = out(k);
y = test_fftNEW(matData,y2);
end
As you can see I wish to name my outputs as fft1 and fft2 I would also like to save the image produced but it was becoming very complicated for me.
My main function is:-
function y = test_fftNEW(matData,y2)
x1=matData;
x=x1(:,2);
win = hamming(length(x));
% win=ones(length(x),1);
y = (x-mean(x));
y1 = y.*win;
NFFT = 2.^nextpow2(length(x));
FFTX = fft(y1,NFFT);
%FFTX = fft(y,NFFT)/length(x);
Fs=60;
figure(1)
%plotting
f = Fs/2*linspace(0,1,NFFT/2);
cyclespersecond=2*abs(FFTX(1:NFFT/2));
%applying a filter
z = cyclespersecond;
RC = (1/(2*pi*0.25));
dt = f(2) - f(1);%just check to see if 1/(NFFT/2)
alpha = (RC/(RC+dt));
n = length(z);
z2(1) = z(1);
for i= 2:n
z2(i)= (alpha * z2(i-1) + alpha * (z(i) - z(i-1)));
end
%note NFFT/2=1 has been changed to NFFT/2
%Plot single-sided amplitude spectrum.
[~,k] = max(cyclespersecond); %new
out2 = f(k);
y = save(y2,out2);
figure(1)
plot(f,cyclespersecond) % only need to plot one half of the spectrum
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
I did also want to save the image each time under a different name but I wasnt sure how to do this anyway the error I keep getting is:
??? Error using ==> test_fftNEW Too many input arguments.
Error in ==> Process_all at 9 y = test_fftNEW(matData,y2);
any help would be appreciated
0 comentarios
Respuestas (1)
dpb
el 6 de Mzo. de 2014
Editada: dpb
el 6 de Mzo. de 2014
out = {'fft1' 'fft2'};
for k = 1:2
...
y2 = out(k);
y = test_fftNEW(matData,y2);
...
As you can see I wish to name my outputs as fft1 and fft2...
No!!!! Don't do this. This way there be dragons (as you've learned). See the wiki for why not and alternative ways to accomplish the same thing.
...
??? Error using ==> test_fftNEW Too many input arguments._
Error in ==> Process_all at 9 y = test_fftNEW(matData,y2);
Yep, 'cuz y2 is the cell array. Type
out(1)
at the command line to see what you're actually passing to the function.
Also, the output of the function will be stored in y on both calls, anyway.
ADDENDUM:
I hadn't read the full code; I see now that you're using y2 in the function as a file with save. I had presumed you thought you were naming an output variable instead...you still have the problem of overwriting y in the return if you want the two there but you can fix the call to create the named output files by changing out = {'fft1' 'fft2'}; to
out = {'fft1'; 'fft2'};
to create a 2x1 cell array. Then, when you use it in the function as
y = save(y2,out2);
since it is a cellstring you must dereference it by char or using the curlies as
y = save(char(y2),out2); % or
y = save({y}2,out2);
Note, however, that all you're actually saving at that point is the one peak location, not the actual fft (unless my eyes are bad and I missed something significant)...
0 comentarios
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!