필터 지우기
필터 지우기

Secify what is the end of a line using textscan?

조회 수: 4 (최근 30일)
kschau
kschau 2013년 5월 8일
I have a .dat file with varied spacing between a series of numbers. I want to be able to jump to any one of these numbers using textscan with its 'headerlines' feature. The problem is, I need to specify that each separate number should be a new line. That way if is use 'headerlines',2 in textscan it will skip the first two numbers and go from there.
My .dat file starts like this
1 55 63 78 45.6 34.6 45.7 23.5 23.7 34.5 23.6 ...
how can I make textscan see line one as 1... line two as 55... line three as 63... line four as 78 etc? I know about the 'EndOfLine' feature and have tried everything with that but it's still not giving me what I need.
Thanks
-Kyle

채택된 답변

Walter Roberson
Walter Roberson 2013년 5월 8일
I would not do it that way. What I would do is something like:
fid = fopen(YourFileName, 'rt');
wantpos = 3; %want 3rd number
fmt = [repmat('%*f', 1, wantpos-1), '%f'];
datacell = textscan(fid, fmt, 1);
wantednum = datacell{1};
fclose(fid);
This can be used even if you have fewer numbers per line than "wantpos": it will read as many lines as it needs to in order to get the proper number.
Afterwards the file will be positioned right after the number that was read.
  댓글 수: 2
kschau
kschau 2013년 5월 8일
Trying to implement your solution but I cant get it to output anything but NaN. Here is my code
fid=fopen('dummy.dat');
wantpos=3;
fmt = [repmat('%*f', 1, wantpos-1), '%f'];
datacell = textscan(fid, fmt, 1);
U = datacell{1}
Thanks for the help!
Walter Roberson
Walter Roberson 2013년 5월 8일
When I use the string you gave, it works for me. Is it certain that the data is separated by spaces? And why did you remove the 'rt' from the fopen() ? Try this:
fid = fopen('dummy.dat', 'r');
data = fread(fid, 20, '*uint8');
fclose(fid)
and then disp(data) and show us what it indicates

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by