I have some (long)text files like this:
1996-002:16.39.23.565 67.060
1996-046:16.42.38.868 67.060
1996-057:17.08.58.620 67.060
1996-080:16.30.06.334 67.060
I want my script to read the first column and the two first numer. It is 1996 which is the year and 002 which is the day of the year. Someone know how to do this? I have tried with:
tmp = textscan(tline, '%s', 'delimiter', ' ', 'MultipleDelimsAsOne', 1);
But i get an error like: Subscripted assignment dimension mismatch. Anyone know what to do?
Thank you.

답변 (1개)

KL
KL 2017년 5월 12일

0 개 추천

fileID = fopen('YourFileName.txt');
C = textscan(fileID,'%d-%d:%s %s');
fclose(fileID);

댓글 수: 7

kk1991
kk1991 2017년 5월 12일
That doesn't work :/
KL
KL 2017년 5월 12일
then attach an example of your txt file. I tested with the data you gave and it works for me.
kk1991
kk1991 2017년 5월 12일
Thank you for helping me. I mean - how can a make this column as a vector in matlab and plot it further. With you suggestion it only describe how long the column is, I guess?
kk1991
kk1991 2017년 5월 12일
This is my code for now. And get this error:
Cell contents reference from a non-cell array object.
Error in tidslinje_test (line 24) d= str2num(C{1})
dinfo= dir('myfile.txt')
N = length(dinfo)
for K = 1 : N
fid= fopen(dinfo(K).name) ;
tline = fgetl(fid);
ii = 0;
while ischar(tline)
ii = ii + 1;
C=textscan(tline,'%d-%d:%s %s')
C=C{1};
d= str2num(C{1})
fclose(fileID)
end
end
KL
KL 2017년 5월 12일
편집: KL 2017년 5월 12일
what exactly are you trying to do?
If your file name is myfile.txt,
>> fileID = fopen('myfile.txt');
C = textscan(fileID,'%d-%d:%s %d');
fclose(fileID);
whos C
Name Size Bytes Class Attributes
C 1x4 1040 cell
>> C{1}
ans =
1996
1996
1996
1996
kk1991
kk1991 2017년 5월 12일
I don't know whats happening, but I'm not getting this results on my computer:
>> C{1}
ans =
1996
21
My text file look like the one i sent you an example of, but just much longer. Don't understand why i get different from you. :/
KL
KL 2017년 5월 12일
attach the txt file here with few rows of data

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

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

질문:

2017년 5월 12일

댓글:

KL
2017년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by