Effacer les filtres
Effacer les filtres

I am having trouble reading a txt file

47 vues (au cours des 30 derniers jours)
Garykalan
Garykalan le 23 Juin 2024 à 1:02
Commenté : dpb le 24 Juin 2024 à 0:36
I have txt file with data formated below but Im only get the first line .
index: 0 int_time: 1718984569904518 us int_diff: 0 us min: 167 us max: 167 us
index: 1 int_time: 1718984569904682 us int_diff: 164 us min: 164 us max: 167 us
index: 1 int_time: 1718984569904682 us int_diff: 164 us min: 164 us max: 167 us
here is the code im using utilising textscan
fid=fopen('Desktop\data.txt','rt');
data=textscan(fid,'%[index: 0 int_time:] %u64 %[us_int_diff: ] %s %s %s %s %s %s %s %s')
fclose(fid);
  1 commentaire
Image Analyst
Image Analyst le 23 Juin 2024 à 1:31
Don't force us create data.txt just to help you. Make it easy for us to help you. If you have any more questions, then please attach your data with the paperclip icon after you read this:

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 23 Juin 2024 à 2:00
You may need to specify the name-value pair:
'EndOfLine','\r\n'
That has usually been the solution when I have encountered similar problems.
That aside, it always helps to have the actual file to work with.
.
  3 commentaires
Star Strider
Star Strider le 23 Juin 2024 à 15:01
We need the original file to check that..
Synthetic files or synthetic data don’t count.
dpb
dpb le 23 Juin 2024 à 16:13
For absolute certainty, yes, but given the symptoms, I'll wager the beer next time... :)

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 23 Juin 2024 à 1:31
Modifié(e) : dpb le 23 Juin 2024 à 16:56
In
%data=textscan(fid,'%[index: 0 int_time:] %u64 %[us_int_diff: ] %s %s %s %s %s %s %s %s')
you're forcing a match on a 0 in "index: 0 int_time:" but only the first record matches that pattern; the remaining two records have a "1", not "0".
Try
fmt='%[index: ] %d %[int_time:] %u64 %[us_int_diff: ] %s %s %s %s %s %s %s %s';
Let's test...
txt=[
'index: 0 int_time: 1718984569904518 us int_diff: 0 us min: 167 us max: 167 us';
'index: 1 int_time: 1718984569904682 us int_diff: 164 us min: 164 us max: 167 us';
'index: 1 int_time: 1718984569904682 us int_diff: 164 us min: 164 us max: 167 us'];
for i=1:size(txt,1)
data{i,1}=textscan(txt(i,:),fmt);
end
data{:}
ans = 1x13 cell array
Columns 1 through 12 {1x1 cell} {[0]} {1x1 cell} {[1718984569904518]} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} Column 13 {1x1 cell}
ans = 1x13 cell array
Columns 1 through 12 {1x1 cell} {[1]} {1x1 cell} {[1718984569904682]} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} Column 13 {1x1 cell}
ans = 1x13 cell array
Columns 1 through 12 {1x1 cell} {[1]} {1x1 cell} {[1718984569904682]} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} Column 13 {1x1 cell}
This is pretty messy mishmash of cells and nested cells; I'd probably at least convert the numeric fields all with %d for the time stamp data. It wouldn't be too hard then to turn the other nested cells into a cellstr containing the string for only one level of dereferencing.
The alternative you might explore would be readtable; your release won't have all the bells and whistles the current does, but it was several years past initial release by then so should be pretty robust. It is a handy container for mixed data types.
fmt='%*[index: ] %d %*[int_time:] %u64 %*[us_int_diff: ]%d %*[us min: ]%d %*[us max: ]%d %*[us]';
for i=1:size(txt,1)
data{i,1}=textscan(txt(i,:),fmt,'collectoutput',0);
end
data=vertcat(data{:})
data = 3x5 cell array
{[0]} {[1718984569904518]} {[ 0]} {[167]} {[167]} {[1]} {[1718984569904682]} {[164]} {[164]} {[167]} {[1]} {[1718984569904682]} {[164]} {[164]} {[167]}
data{:,2}
ans = uint64 1718984569904518
ans = uint64 1718984569904682
ans = uint64 1718984569904682
whos ans
Name Size Bytes Class Attributes ans 1x1 8 uint64
tD=cell2table(data,'VariableNames',{'Index','Time','Diff','Min','Max'})
tD = 3x5 table
Index Time Diff Min Max _____ ________________ ____ ___ ___ 0 1718984569904518 0 167 167 1 1718984569904682 164 164 167 1 1718984569904682 164 164 167
  2 commentaires
Garykalan
Garykalan le 23 Juin 2024 à 23:38
fmt='%[index: ] %d %[int_time:] %u64 %[us_int_diff: ] %s %s %s %s %s %s %s %s';
this format actually worked out i was able to use the cell2mat function and everything is perfect thank you so much your a god
dpb
dpb le 24 Juin 2024 à 0:36
"...i was able to use the cell2mat function and everything is perfect..."
Are you sure? Be careful; I think you'll find that turned everything to double and you'll not have the correct value for the Time U64 field data. I got bit by that in one of my examples earlier...
However, if this did solve your problem, please go ahead and Accept the answer so it is apparent if nothing else.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Tags

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by