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

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
Dennis
Dennis 2018년 6월 14일
편집: Dennis 2018년 6월 14일
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

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2018년 6월 14일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by