Subtraction of Two sequences
Show older comments
Hello everyone ,
I am very new to this programming language ,I have started writing simple programs and encountered following errors
1. Input argument "n1" is undefined.
*Error in ==> subseq1 at 6 n=(min(min(n1),min(n2)): max(max(n1),max(n2))); * And yet if I try to give the ouptut as the foll. subseq1(-3:3,[3 :2 :15],0 :4,2:6)
and command window displays all the variables with assigned values ..and shows the signal sub. But My query is Can I debug the error? if So How? Below is the code for reference :
{
%x1=first sequence of range n1
%x2=second sequence of range n2
function[y1,y2,a,b]= subseq1(n1, x1, n2,x2) clc; n=(min(min(n1),min(n2)): max(max(n1),max(n2)));
y1=zeros(1,length(n)) y2=y1 a=(n>=min(n1))&(n<=max(n1)) b=(n>=min(n2))&(n<=max(n2)) y1(a)=x1 y2(b)=x2 y=y1-y2 subplot(3,1,1) stem(n1,x1) title('sequence x1') xlabel('n1------------' ) ylabel('x1------------ ') subplot(3,1,2) stem(n2,x2) title('sequence x2') xlabel('n2------------ >') ylabel('x2------------ >') subplot(3,1,3) stem(n,y) title('subtraction of 2 sequence' ) xlabel('n------------ >') ylabel('y------------ >')
Please help me !
2 Comments
TAB
on 21 Oct 2011
Please format your code to make it readable.
For answer please click the link below to view the complete article. Subtraction of Sequences in MATLAB
Answers (2)
Wayne King
on 21 Oct 2011
Hi, please format your code for readability in future posts.
Save the function subseq1.m in a folder on the MATLAB path and then call it at the command line as:
[y1,y2,a,b] = subseq1(-3:3,[3 :2 :15],0 :4,2:6);
Here is your function:
function[y1,y2,a,b]= subseq1(n1, x1, n2,x2)
n=(min(min(n1),min(n2)): max(max(n1),max(n2)));
y1=zeros(1,length(n));
y2=y1;
a=(n>=min(n1))&(n<=max(n1));
b=(n>=min(n2))&(n<=max(n2));
y1(a)=x1;
y2(b)=x2;
y=y1-y2;
subplot(3,1,1)
stem(n1,x1);
title('sequence x1');
xlabel('n1------------' ); ylabel('x1------------ ');
subplot(3,1,2);
stem(n2,x2);
title('sequence x2');
xlabel('n2------------ >'); ylabel('x2------------ >');
subplot(3,1,3);
stem(n,y);
title('subtraction of 2 sequence' );
xlabel('n------------ >');
ylabel('y------------ >');
2 Comments
Parul
on 21 Oct 2011
Wayne King
on 21 Oct 2011
Then you're doing something wrong, because the above works perfectly for me. You're probably still calling your old subseq1.m.
Do this:
1.) Delete your old subseq1.m
2.) Copy and paste what I have given you into a new subseq1.m file and save that file in a folder, let's call it c:\mfiles
3.) in the MATLAB command window, >>cd c:\mfiles
4.) Enter >>pwd to make sure you are in that directory
5.) Enter
>> [y1,y2,a,b] = subseq1(-3:3,[3 :2 :15],0 :4,2:6);
Sk Group
on 8 Feb 2021
0 votes
MATLAB CODE:
function [g n] = sigsubtract(x1,n1,x2,n2)
if n1(1)< n2(1)
a = n1(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
else
a = n2(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
end
if n1(end)>n2(end)
b = n1(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
else
b = n2(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
end
n = a:b;
g = x1-x2;
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!