I keep getting A(I) = B, the number of elements in B and I must be the same. matlab error

2 vues (au cours des 30 derniers jours)
John
John le 29 Nov 2013
Commenté : Image Analyst le 29 Nov 2013
last=0;
upflag=0;
p=zeros(length(detsq),1);
for i=1:length(detsq)
if(detsq(i)>1000)
if(upflag ==0)
if(last>0)
t= i-last;
p=1000/t*60
end
last=i;
end
upflag=100;
else
if(upflag>0)
upflag=upflag-1;
end
end
pulse(i)=p;
end
figure(3),plot(pulse)
In an assignment A(I) = B, the number of elements in B and I must be the same
This is the error keep popping. Any experts who can help me identify where I went wrong?

Réponses (4)

Image Analyst
Image Analyst le 29 Nov 2013
Is it your intention that p should be a vector, like you said here: p=zeros(length(detsq),1);
or is it your intention that p should be a scalar like you say here: p=1000/t*60
If you do this: pulse(i)=p; then p must be a scalar. Though I'm just guessing at the line of code producing the error because, for some strange reason, you left out that crucial part of the error message that identifies the line of code that caused the error.
  2 commentaires
John
John le 29 Nov 2013
%Step 8
det =filter (b2, 1, firs6);
figure(17),plot (det)
detsq=det .^2;
figure(18),plot(detsq)
last=0;
upflag=0;
p=zeros(length(detsq),1);
for i=1:length(detsq)
if(detsq(i)>0.1) %threshold
if(upflag ==0)
if(last>0)
t= i-last;
p=1000/t*60 %pulse rate
end
last=i;
end
upflag=100;
else
if(upflag>0)
upflag=upflag-1;
end
end
pulse(i)=p;
end
figure(19),plot(pulse)
I run again and line 25 which is pulse(i)=p the cause of the error. I show my full code for the whole program.
Image Analyst
Image Analyst le 29 Nov 2013
Undefined function or variable 'b2'.
Error in test (line 3)
det =filter (b2, 1, firs6);

Connectez-vous pour commenter.


John
John le 29 Nov 2013
%step 3
function output = myFIR1(input,hn)
global in
in = [in input];
output =0;
r =(length(hn))*(length(in)>=length(hn))+(length(in))*(length(in)<length(hn));
for C = 0:r-1
output = output+(in(length (in)-C)*hn(C+1));
end
end

John
John le 29 Nov 2013
Modifié(e) : John le 29 Nov 2013
load 'ecg_8.dat'
y=ecg_8(:,2);
%Step 2
A=ecg_8(:,2)-mean(ecg_8(:,2));
fs=1000; %Sampling Frequency
t=0:1/fs:(length(ecg_8)-1)/fs;
B=((4.096+4.096)/4096)/1000; %converts data to millivolts
plot(t*1000,A*B*1000)
y2=fft(A);
L=length(y2);
freq=(0:fs/(L-1):fs);
plot(freq,abs(y2));
axis([0 500 0 3000000])
%step 4
n=(-100:100); %Set number of taps
w1=2*pi*0.03; %Cut-off frequency 1
w2=2*pi*0.08; %Cut-off frequency 2
h4=(1./(n*pi)).*(sin(w1*n)-sin(w2*n)); %Impulse Response
h4(101)=1+(w1-w2)/pi; %Sets middle value
h4=h4 .*blackman(201)'; %Mutilpied by Blackman function
figure(1);subplot(2,1,1),plot(20*log10(abs(fft(h4)))); %Plot in decibel against frequency
axis([-50 1050 -30 10]);
subplot(2,1,2),plot(h4);
for row=1:length(A)
firs4(row)=myFIR1(A(row),h4);
end
clearvars in row
figure(2),plot(firs4),title('Step 4 Impulse Response');
%step 5
n1=(-200:200);
ws=2*pi*0.008;
h5=-(sin(ws.*n1)./(n1*pi));
h5(201)=1-(ws/pi);
h5=h5 .*blackman(401)';
figure(3),subplot(2,1,1),plot(20*log10(abs(fft(h5))));
axis([-50 1050 -0.1 1.1]);
subplot(2,1,2),plot(h5);
for row=1:length(firs4)
firs5(row)=myFIR1(firs4(row),h5);
end
clearvars in row
figure(4),plot(firs5),title('Step 5')
xlabel('time (ms)'),ylabel('voltage(mv)')
%step 6
h6(1:501)=1;
h6(1:2)=0;
h6(500:501)=0;
h6(36:66)=0;
h6(501-65+1:501-35+1)=0;
plot(h6)
h=real(ifft(h6));
plot(h)
h2(1:251)=h(251:501);
h2(251:501)=h(1:251);
plot(h2);
h3=h2.*blackman(501)';
figure(5),plot(h3),title('Impulse Response');
xlabel('Frequency (Hz)'),ylabel('Ampltitude');
for row=1:length(firs5)
firs6(row)=myFIR1(firs5(row),h3);
end
clearvars in row
figure(6),plot(firs6),title('Step 6')
xlabel('Time(ms)'),ylabel('Voltage(mv)')
%Step 7
H=(A)*(B)*(1000);
hb1=H(4000:4600);
figure(7),plot(hb1)
b=flipud(hb1);
figure(8),plot(b) %Original Graph
xlabel('Frequency (Hz)'),ylabel('Amplitude'),title('Original Hb')
det=filter(b,1,H); %Original det vs Orignal Hb
figure(9),plot(det)
det=filter(b,1,firs5); %Original vs Step 5
figure(10),plot(det)
det=filter(b,1,firs6); %Original vs Step6
figure(11),plot(det)
w=firs6;
plot(w)
hb2=w(2200:2800);
figure(12),plot(hb2)
b2=fliplr(hb2);
figure(13),plot(b2) %Perfect Graph
det=filter(b2,1,H);
figure(14),plot(det) %Perfect vs original
det=filter(b2,1,firs5);
figure(15),plot(det) %Perfect vs step 5
det=filter(b2,1,firs6);
figure(16),plot(det) %Perfect vs step 6
  5 commentaires
John
John le 29 Nov 2013
Modifié(e) : John le 29 Nov 2013
Sir, what is the exact problem that causes the program not able to run the last step? Can you let me know which one needs to change? Thank you!
Image Analyst
Image Analyst le 29 Nov 2013
You can't have negative or fractional indexes, OR stuff a bunch of elements into a single element. Step through your code until it throws an error, then examine all the variables and see what violates what I said in the first sentence of this comment.

Connectez-vous pour commenter.


John
John le 29 Nov 2013
Hi, I attached the ecg_8 signal to load into matlab

Community Treasure Hunt

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

Start Hunting!

Translated by