Why i get "Unexpected Matlab expression"?

function [Drcode, timer, lat, lon, u, v] = read_drifter_1('cc1.dat' , '0');
%
% usage [Drcode, timer, lat, lon, u, v] = read_drifter_1(filename, plotflag)
% this routine reads the data from the .dat files produced by the Android
% Drifter application, and provides the data.
%
fid = fopen('cc1.dat');
A = fgetl(fid);
timer=[];
lat=[];
lon=[];
u = [];
v = [];
while ~feof(fid),
A = fgetl(fid);
s = findstr(A,' ');
N = length(s);
Rlat = str2num(A(s(1)+1:s(2)-1));
Rlon = str2num(A(s(2)+1:s(3)-1));
Ryy = str2num(A(s(5)+1:s(6)-1));
Rmm = str2num(A(s(6)+1:s(7)-1));
Rdd = str2num(A(s(7)+1:s(8)-1));
Rhh = str2num(A(s(8)+1:s(9)-1));
Rmin = str2num(A(s(9)+1:s(10)-1));
Rsec = str2num(A(s(10)+1:length(A)));
Rtime = datenum(Ryy, Rmm, Rdd, Rhh, Rmin, Rsec);
lat = [lat; Rlat];
lon = [lon; Rlon];
timer = [timer; Rtime];
end
%
% Start a quality control regarding times
% Sort the data in increasing time
[timer2, Itime] = sort(timer,'ascend');
lat2 = lat(Itime);
lon2 = lon(Itime);
% remove the data taken at exactly the same time (double records)
dt = diff(timer2);
J = find(dt > 0);
timer = timer2(J+1);
lat = lat2(J+1);
lon = lon2(J+1);
% %
fclose(fid);
Drcode = [];
if plotflag ~=0,
figure
plot(lon2, lat2,'.')
hold on
plot(lon, lat, 'o')
end
return
Christos

5 Comments

function [Drcode, timer, lat, lon, u, v] = read_drifter_1('cc1.dat' , '0');
%
% usage [Drcode, timer, lat, lon, u, v] = read_drifter_1(filename, plotflag)
fid = fopen('cc1.dat');
A = fgetl(fid);
timer=[];
lat=[];
lon=[];
u = [];
v = [];
while ~feof(fid),
A = fgetl(fid);
s = findstr(A,' ');
N = length(s);
Rlat = str2num(A(s(1)+1:s(2)-1));
Rlon = str2num(A(s(2)+1:s(3)-1));
Ryy = str2num(A(s(5)+1:s(6)-1));
Rmm = str2num(A(s(6)+1:s(7)-1));
Rdd = str2num(A(s(7)+1:s(8)-1));
Rhh = str2num(A(s(8)+1:s(9)-1));
Rmin = str2num(A(s(9)+1:s(10)-1));
Rsec = str2num(A(s(10)+1:length(A)));
Rtime = datenum(Ryy, Rmm, Rdd, Rhh, Rmin, Rsec);
lat = [lat; Rlat];
lon = [lon; Rlon];
timer = [timer; Rtime];
end
%
% Start a quality control regarding times
% Sort the data in increasing time
[timer2, Itime] = sort(timer,'ascend');
lat2 = lat(Itime);
lon2 = lon(Itime);
% remove the data taken at exactly the same time (double records)
dt = diff(timer2);
J = find(dt > 0);
timer = timer2(J+1);
lat = lat2(J+1);
lon = lon2(J+1);
% %
fclose(fid);
Drcode = [];
if plotflag ~=0,
figure
plot(lon2, lat2,'.')
hold on
plot(lon, lat, 'o')
end
return
Thank you. I am totally new
Christos
Try to define your function like this
function [Drcode, timer, lat, lon, u, v] = read_drifter_1(mystring , plotflag) %no ''
%do something with your input
fid = fopen(mystring);
...
end
and then you can call it:
read_drifter('cc1.dat',0)
function [Drcode, timer, lat, lon, u, v] = read_drifter_1(filename, plotflag)
fid = fopen(filename);
A = fgetl(fid);
timer=[];
lat=[];
lon=[];
u = [];
v = [];
while ~feof(fid),
A = fgetl(fid);
s = findstr(A,' ');
N = length(s);
Rlat = str2num(A(s(1)+1:s(2)-1));
Rlon = str2num(A(s(2)+1:s(3)-1));
Ryy = str2num(A(s(5)+1:s(6)-1));
Rmm = str2num(A(s(6)+1:s(7)-1));
Rdd = str2num(A(s(7)+1:s(8)-1));
Rhh = str2num(A(s(8)+1:s(9)-1));
Rmin = str2num(A(s(9)+1:s(10)-1));
Rsec = str2num(A(s(10)+1:length(A)));
Rtime = datenum(Ryy, Rmm, Rdd, Rhh, Rmin, Rsec);
lat = [lat; Rlat];
lon = [lon; Rlon];
timer = [timer; Rtime];
end
%
% Start a quality control regarding times
% Sort the data in increasing time
[timer2, Itime] = sort(timer,'ascend');
lat2 = lat(Itime);
lon2 = lon(Itime);
% remove the data taken at exactly the same time (double records)
dt = diff(timer2);
J = find(dt > 0);
timer = timer2(J+1);
lat = lat2(J+1);
lon = lon2(J+1);
% %
fclose(fid);
Drcode = [];
if plotflag ~=0,
figure
plot(lon2, lat2,'.')
hold on
plot(lon, lat, 'o')
end
return
The original Function
Thanks for answer
Christos
Solved
Thanks Davis
Christos

Sign in to comment.

 Accepted Answer

It looks like you haven't really understood how to define functions and how to use them.
The signature of a function is
function [outputvar1, outputvar2, ...] = functionname(inputvar1, inputvar2, ...)
where outputvarx and inputvarx are variable names that you then use in the body of your function. They must be variable names, you can't replace these by actual value. Strangely enough you've got it right in the function documentation. Your function signature should be
function [Drcode, timer, lat, lon, u, v] = read_drifter_1(filename, plotflag)
When you want to use that function, you don't go and edit that signature and write the values to use in there. Instead you call that function from the command line, a script, or another function. For example, if you want to use that function with the inputs 'cc1.dat' and 0, you'll call it with the line:
read_drifter_1('cc1.dat' , 0)
or if you want to see the outputs:
[somename, someothername, anothername, outputforlon, outputforu, outputforv] = read_drifter_1('cc1.dat' , 0)
Some other notes:
  • It's not an error but the return at the end of the function is unnecessary. Remove it.
  • The value you pass for plotflag should be logical or numerical, not a char array. You're meant to pass either true, false, 0, or 1, but not '0' or '1'. Both '0' and '1' would have the same effect as if you've passed true.

1 Comment

True
I really am just starting learning.
Thanks for your very helpfull comment.
And sorry for the trouble.
Christos

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics and Optimization 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!