using findstr to find what you are looking for

2 vues (au cours des 30 derniers jours)
Christina Reid
Christina Reid le 2 Avr 2021
Modifié(e) : dpb le 2 Avr 2021
Here is a function that is suppose to take a datetime, reformat it in a way where it can search for a date on the following webiste, https://www.celestrak.com/SpaceData/EOP-Last5Years.txt. I am getting an error 'Error using findstr
Conversion to double from datetime is not possible.' and do not know how to fix it.
Here is the script:
datestr = datetime('now');
%startdatestr = datestr(get(handles.startedit,'string'),'yyyy mm dd');
startdatestr = datetime (('now'), 'Format','yyyy MM dd')
internet = inputdlg('Are you connected to the internet? (y/n)','Connectivity');
if(strcmp(internet,'y'))
eopdata = urlread('https://www.celestrak.com/SpaceData/EOP-Last5Years.txt');
ind = findstr(eopdata,startdatestr);
xp = str2num(eopdata(ind+18:ind+26))*convrt;
yp = str2num(eopdata(ind+28:ind+36))*convrt;
dut1 = str2num(eopdata(ind+38:ind+47));
lod = str2num(eopdata(ind+49:ind+58));
% dPsi = str2num(eopdata(ind+60:ind+68));
% dEpsilon = str2num(eopdata(ind+70:ind+78));
% dx = str2num(eopdata(ind+80:ind+88));
% dy = str2num(eopdata(ind+90:ind+98));
dat = str2num(eopdata(ind+100:ind+102));
else
localeopfile = inputdlg('Do you have the latest EOP file from https://www.celestrak.com/SpaceData/EOP-Last5Years.txt (y/n)','EOP File');
if(strcmp(localeopfile,'y'))
[filename,pathname] = uigetfile('*.txt', 'Select an EOP file');
if(isempty(filename))
return;
end
fid = fopen(fullfile(pathname,filename));
datastartstr = 'BEGIN OBSERVED';
while 1
tline = fgetl(fid);
if(strcmp(datastartstr,tline))
break;
end
end
while 1
tline = fgetl(fid);
if(~isempty(tline))
if(strcmp(startdatestr,tline(1:10)))
break;
end
end
end
eopdata = tline;
xp = str2num(eopdata(18:26))*convrt;
yp = str2num(eopdata(28:36))*convrt;
dut1 = str2num(eopdata(38:47));
lod = str2num(eopdata(49:58));
% dPsi = str2num(eopdata(60:68));
% dEpsilon = str2num(eopdata(70:78));
% dx = str2num(eopdata(80:88));
% dy = str2num(eopdata(90:98));
dat = str2num(eopdata(100:102));
else
xp=0;
yp=0;
lod=0;
dut1=0;
dat=0;
msgbox('EOP data set to zeros. EOP file can be downloaded from http://celestrak.com/SpaceData');
end
end

Réponse acceptée

dpb
dpb le 2 Avr 2021
Modifié(e) : dpb le 2 Avr 2021
Looks like hard way to go at it to me...I'd just download the data to local file and snarf it up ...
eopdata = urlread('https://www.celestrak.com/SpaceData/EOP-Last5Years.txt');
fid=fopen('EOP-Last5Years.txt','w');
fwrite(fid,eopdata,"uchar");
fid=fclose(fid);
eopdata=split(eopdata,newline); % convert to cell array from char string
ixVNames=find(contains(eopdata,'MJD')); % find the record with the variable names as marker
tEOP=readtable('EOP-Last5Years.txt','NumHeaderLines',ixVNames+7); % I just counted here...
tEOP.Date=datetime(tEOP.Var1,tEOP.Var2,tEOP.Var3);
vNames=split(strtrim(extractBetween(eopdata(ixVNames),'#',char(13))));
tEOP=tEOP(:,[end 5:end-1]);
tEOP.Properties.VariableNames=vNames([1 3:end]);
Above left with
>> head(tEOP)
ans =
8×10 table
Date x y UT1-UTC LOD dPsi dEpsilon dX dY DAT
___________ ____ ____ _______ ____ _____ ________ _____ _____ _____
01-Jan-2016 0.05 0.26 0.08 0.00 -0.09 -0.01 -0.00 0.00 36.00
02-Jan-2016 0.05 0.26 0.08 0.00 -0.09 -0.01 -0.00 0.00 36.00
03-Jan-2016 0.05 0.26 0.08 0.00 -0.09 -0.01 -0.00 0.00 36.00
04-Jan-2016 0.05 0.26 0.08 0.00 -0.09 -0.01 -0.00 -0.00 36.00
05-Jan-2016 0.04 0.26 0.07 0.00 -0.09 -0.01 -0.00 -0.00 36.00
06-Jan-2016 0.04 0.26 0.07 0.00 -0.09 -0.01 -0.00 -0.00 36.00
07-Jan-2016 0.04 0.26 0.07 0.00 -0.09 -0.01 -0.00 -0.00 36.00
08-Jan-2016 0.04 0.27 0.07 0.00 -0.09 -0.01 -0.00 -0.00 36.00
>>
One can read the header info and do whatever it is that is needed with the NGA_COEFFICIENTS to use the data in similar manner or use textscan to parse the in-memory copy.
At this point you can do all your searching for times directly on the datetime Date value; much simpler than finding strings.
  8 commentaires
dpb
dpb le 2 Avr 2021
Modifié(e) : dpb le 2 Avr 2021
ADDENDUM
" the date format from the website and Matlab do not exactly match. "
While searching for dates by matching strings is a fools errand, you can set the display format for the .Date field however you want it to look.
But, there's no reason to be going back to the web site; you've got all the data in hand and know how to get more any time you want it.
It's much more efficient to just load it all and use it than it would be to try to add on the next day's readings or such by trying to only download what you don't already have--just get it all.
If the archive "rolls off the top" and loses past history, then save your data to a .mat file and augment it if needed.
But, even if you want to add to the existing, do it by downloading everything the way we just demonstrated, then select dates after the last in the original that are in the new and add them is the way to do it. join and friends are useful there.
dpb
dpb le 2 Avr 2021
ADDENDUM 2:
OBTW, there are some missing records it seems; you'll probably want to clean up the data as follows:
>> tail(tEOP) % original -- note last record
ans =
8×10 table
Date x y UT1-UTC LOD dPsi dEpsilon dX dY DAT
___________ _______ _______ _________ __________ ________ _________ ________ _________ ___
...
29-Sep-2021 0.21526 0.30694 -0.055945 -0.0012821 -0.11764 -0.008932 0.000215 -0.000134 37
30-Sep-2021 0.21422 0.30573 -0.054636 -0.0012521 -0.11746 -0.009039 0.000213 -0.00012 37
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN
>> sum(isnat(tEOP.Date)) % how many are there altogether?
ans =
4
>> tEOP(isnat(tEOP.Date),:) % look to see if there's data or what's going on...
ans =
4×10 table
Date x y UT1-UTC LOD dPsi dEpsilon dX dY DAT
____ ___ ___ _______ ___ ____ ________ ___ ___ ___
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN
>> tEOP=tEOP(~isnat(tEOP.Date),:); % all are null records so delete 'em all
>> whos tEOP % who's left info...
Name Size Bytes Class Attributes
tEOP 2100x10 170918 table
>>

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Install Products dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by