Textscan import string data from .txt file

1 vue (au cours des 30 derniers jours)
Linus Dock
Linus Dock le 13 Nov 2021
Commenté : dpb le 17 Nov 2021
Hi!
When I'm using textscan to read my data I get all the data but it's not quite organized the way I would like.
I will attach a sample .txt file.
I'm using this code to import the data:
%Imports all .txt files according user input time and convert into strings
Data = cell(1, numfiles); %Preallocate empty cell
for h = 1:numfiles
filename = sprintf('%s.txt',w(h,:)); %add .txt to year and month
fileID = fopen(filename); %open filename to create fileID
Data{h} = textscan(fileID,'%s','delimiter','\n'); %read all characters in fileID
fclose(fileID); %close fileID
end
What I would like to achieve is a string starting with METAR ESXX and with varying ending (for ex. Q1011 or R08/750135 or other).
I've tried using different delimiters but I get more or less the same result with the different delimiters.
It seems to be some problem when the data is not delimited by a newline what I can tell, but I can't find the right solution to get it working.
In a previous version of my code I was using fread but I understand that textscan is better to use. Is that correct?
Do you have any suggestions to what could be changed?
Thanks!
This a sample of the result of Data.
'M04/M06 Q1020
METAR ESKN 160020Z 31003KT 0300 R08/P2000N R26/1100N BCFG NSC'
'M04/M05 Q1010 R08/750135
METAR ESKN 160050Z 31003KT 5000 BR FEW064 M04/M04 Q1011 R08/750135
METAR ESKN 160120Z 31003KT CAVOK M03/M03 Q1011 R08/750135
METAR ESKN 160150Z VRB01KT 9999 FEW003 BKN061 M03/M03 Q1011'
'R08/750135
METAR ESKN 160220Z 32004KT 9999 SCT042 BKN055 BKN066 M02/M02 Q1011'
'R08/750135
METAR ESKN 160250Z 28003KT 9999 SCT003 BKN036 BKN057 M02/M02 Q1012'
'R08/750135
METAR ESKN 160320Z VRB02KT 9999 BKN002 M02/M02 Q1012 R08/750135
METAR ESKN 160350Z 33004KT 9999 BKN002 M01/M01 Q1012 R08/750135
METAR ESKN 160420Z VRB01KT 9999 BKN002 M01/M01 Q1012 R08/750135
METAR ESKN 160450Z 00000KT 4000 BR SCT003 M02/M02 Q1013 R08/710195
METAR ESKN 160520Z 30003KT 0300 R08/P2000N R26/0750U BCFG FEW003'
'SCT072 M03/M03 Q1013 R26/710195
METAR ESKN 160550Z VRB03KT 9000 SCT066 M03/M03 Q1013 R26/710195
METAR ESKN 160620Z 29003KT 9999 FEW002 BKN068 M02/M02 Q1013'
'R26/710195
METAR ESKN 160650Z 31003KT 9999 FEW002 SCT068 M00/M00 Q1014'

Réponse acceptée

dpb
dpb le 13 Nov 2021
Read the file as is and then clean it up instead...
d=readcell('202103.txt','Delimiter',newline); % read a cellstr array
i1=find(~startsWith(d,'METAR'))-1; % locate first of line pairs
for i=1:numel(i1) % and merge those by pair
d(i1(i))=join(d(i1(i):i1(i)+1));
end
d(i1+1)=[]; % then eliminate the second
Sanity check...
>> all(startsWith(d,'METAR'))
ans =
logical
1
>>
  9 commentaires
Linus Dock
Linus Dock le 17 Nov 2021
Awesome! There was joy!
Importdata did the trick.
This is what worked for me
Data = cell(1, numfiles); %Preallocate empty cell
for h = 1:numfiles
filename = sprintf('%s.txt',w(h,:));
d=importdata(filename);
i1=find(~startsWith(d,'METAR'))-1;
for i=1:numel(i1)
d(i1(i))=join(d(i1(i):i1(i)+1));
end
d(i1+1)=[]; % then eliminate the second
Data{h}=d; % will save into your large array
end
Thanks a lot!
dpb
dpb le 17 Nov 2021
I was sure it would... :)
Glad to help; sorry didn't know were on earlier release initially...

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by