Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with script

Status
Not open for further replies.

brentaar

Computer
Nov 30, 2004
3
i have a project and every time i run it it gives me this "??? Error: File: C:\MATLAB7\work\square.m Line: 15 Column: 26
Missing variable or function." i have spent many days and i still can't figure it out. there are three parts to the script the main (square.m), the function (fsum), and the given material (pwrspec.m). the problem doesn't make sense, at least to a novice like me. here is the all of the script:

square.m

clf
%trying to find a square wave
time = [0:0.01:10];
T=10;
omega=2*pi*1/T;
fsummation=0
k=1;
gc = ['b','c', 'm', 'k', 'y','g', 'r', 'b'];

for ndx=[1:2:19],
farray(k,:) = fsum(time, omega, ndx);
subplot(10,2,ndx)
for m=1:k,
hold on
gc(mod(k,8)+1)
plot(time,farray(m,:)’gc')
end
fsummation = fsummation + farray(k,:)
subplot(10,2,ndx+1)
plot(time, fsummation’r’)
k=k+1;
end
gtext('Archie Brentano SN')

figure
pwrspec(fsummation, 1000,100)
gtext(‘other name’)

fsum.m

function fsum(time, omega, ndx)
%fsummation
fsum = 4/pi*1/ndx * sin(ndx*omega*time)

pwrspec.m

function pwrspec(signal, N, srate)
% pwrspec(signal, N, srate)
% pwrspec computes the power spectrum of a signal and creates a plot
% signal = vector containing signal values
% N = desired length of vector, normally obtained from "length()" function
% srate = sampling rate used to calculate signal, typically twice the value of
% highest frequency

P = fft(signal,N);
n = length(P);
plen = floor(n/2);
power = abs(P(1:plen)).^2;
nyquist = 1/2 * srate;
freq = (1:plen)/(plen)*nyquist;

stem(freq,power)
title('Power spectrum of signal stream')
xlabel('frequency (Hz)')
ylabel('signal intensity')
grid
 
Replies continue below

Recommended for you

Looks like it doesn't like your plot(time,farray(m,:)’gc')

I'd guess a missing comma.

Cheers

Greg Locock
 
putting the comma in, just changes the error say: "??? Error: File: C:\MATLAB7\work\square.m Line: 16 Column: 27
Missing variable or function." so that doesn't seem to change the problem all that much

square.m

clf
%trying to find a square wave
time = [0:0.01:10];
T=10;
omega=2*pi*1/T;
fsummation=0
k=1;
gc = ['b','c', 'm', 'k', 'y','g', 'r', 'b'];

for ndx=[1:2:19],
farray(k,:) = fsum(time, omega, ndx);
subplot(10,2,ndx)
for m=1:k,
hold on
gc(mod(k,8)+1)
plot(time,farray(m,:),’gc')
end
fsummation = fsummation + farray(k,:)
subplot(10,2,ndx+1)
plot(time, fsummation’r’)
k=k+1;
end
gtext('Archie Brentano SN')

figure
pwrspec(fsummation, 1000,100)
gtext(‘other name’)
 
Well, the advantage of a modular language is that you can go into debug mode. Write some routines that test each subroutine by itself. Comment out the parts of the code that are not essential to its function, such as plot...

Incidentally I can't say I like the look of gc(mod...

either but that may be you doing something fancy to an array of strings, not the sort of thing I do.




Cheers

Greg Locock
 
There are some errors spread out through your code

[ol]
[li]You use instead of ' several times.[/li]
[li]The fsum function has a VB-like syntax that doesn't work in matlab.
Code:
function out = fsum(time, omega, ndx)
%fsummation
out = 4/pi*1/ndx * sin(ndx*omega*time);
is much better.[/li]
[li]As Greg said, gc(mod(k,8)+1) is wrong. Something like this is better.
Code:
color=gc(mod(k,8)+1);
plot(time,farray(m,:),color)
[/li]
[/ol]
The pwrspec.m doesn't seem to have any errors.

This was what I found when giving it a quick look, there might be more problems.
 
well i finally got it to work. here is the code if anyone wants to know:

square.m

clf
%trying to find a square wave
time = [0:0.01:10];%gives the increments of measuring for time
T=10;%the amount of time it takes to run through the run
omega=2*pi*1/T;
fsummation=0;%starting parameter for the right graph
k=1;%starting inner and outer variable
gc = [ 'k', 'b', 'g', 'r', 'c', 'm', 'y'];%color array
gh = 1;%labels the title of Square wave
gp = 1;%labels the title of partial series
u = [0, 10, -1.25, 1.25]%limits the axis of the graphs
pr = [0, 6, 0, 60000]
for ndx=[1:2:19],%this is the index which tells the script to run on the odd numbers
farray(k,:) = fsum(time, omega, ndx);
subplot(10,2,ndx);

for m=1:k;
color=gc(mod(m,7)+1);
plot(time, farray(m,:), color);
hold on

if (gp==1)
title('Partial series: 1,3,5,7,9,11,13,15,17,19')
end
gp = gp + 1


axis(u)

if (ndx==19)
xlabel('sec')
end

grid on
end

fsummation = fsummation + farray(k,:);
subplot(10,2,ndx+1);
plot(time, fsummation, 'r');
k=k+1;
axis(u)
if (ndx==19)


xlabel('sec')
end
grid on

if (gh == 1)
title('Square wave synthesis')
end
gh = gh + 1

end
gtext('Archie Brentano SN: 930-584-308 ECE111 Fall 2004 ')



figure
pwrspec(fsummation, 1000,100)
axis(pr);
gtext('Archie Brentano SN: 930584-308 ECE111 Fall 2004')

fsum.m

function out = fsum(time, omega, ndx)
%fsummation
%time is a time-axis vector
%rfreq is the radian frequency
%idx is the square wave synthesis degree index
%fsum will calculate a term of a series of sine waves that will approach a square wave
out = 4/pi*1/ndx * sin(ndx*omega*time)

pwrspec.m

function pwrspec(signal, N, srate)
% pwrspec(signal, N, srate)
% pwrspec computes the power spectrum of a signal and creates a plot
% signal = vector containing signal values
% N = desired length of vector, normally obtained from "length()" function
% srate = sampling rate used to calculate signal, typically twice the value of
% highest frequency

P = fft(signal,N);
n = length(P);
plen = floor(n/2);
power = abs(P(1:plen)).^2;
nyquist = 1/2 * srate;
freq = (1:plen)/(plen)*nyquist;

stem(freq,power)
title('Power spectrum of signal stream')
xlabel('frequency (Hz)')
ylabel('signal intensity')
grid

thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor