Import and modify " .txt" files

Hi
I have some ".txt" files with this structure :
useless
useless
useless
...
data
data
...
useless
useless
useless
data
...
And I would like to have a .txt file like this :
data
data
data
...
I just know that each first line of data section begins with '1' but the length of the headerline (useless) is not fixed ! So I would like to iterate until lines begins with '1' and then keep the data.
How would you do that the easiest way ?
Thanks a lot

5 个评论

How can you tell when you have reached the end of a block of data?
What is the format of each line of the data section? Is it a single numeric value per line?
Data comes from a measurement In the 'useless' section, I got the number N of measurements points (with strfind....). And N is actually the length of each data block. So I would like Matlab to delete lines until line begins with '1' then to keep lines until the end of the data block ..
Data section is a three or four columns (dépends of the data block) of numerical values
It would be best if you provide a sample file.
Here is a sample file. As you can see 'X-Axis Size' gives me the number of measurement points (10) and I still have this value with some code (strfind...)
Sorry, here it is... And file2 is what I want
Many thanks

请先登录,再进行评论。

回答(2 个)

Copy those lines that have four numbers:
fid = fopen('file.txt', 'r');
fid2 = fopen('file2.txt', 'w');
line = fgets(fid);
while line ~= -1
[~, count] = sscanf(line, '%f');
if count == 4
fprintf(fid2, '%s', line);
end
line = fgets(fid);
end
fclose(fid)
fclose(fid2)

6 个评论

Thanks. I have just tried...
'file2.txt' has a single line which is the first line of my data section.. :(
johnmay
johnmay 2015-11-26
编辑:Walter Roberson 2022-7-31
You Genius ! Many thanks ! !
That's weird. It works smoothly for me. You could add a
disp(line)
disp(count)
pause
in the loop and see if the lines are properly detected.
Is it possible do this with only one file ?
You mean reading and write to the same file? No, I don't think so.
Last question : when using 'fprintf' is there a way to write only the 'x' first columns of my line? and not the whole line So that the final file has a constant number of columns and could be opened easily ?
Many thanks

请先登录,再进行评论。

johnmay
johnmay 2015-11-26

0 个投票

Last question : when using 'fprintf' is there a way to write only the 'n' first columns of my line? and not the whole line. So that the final file has a constant number of columns (n) and could be opened easily ?
Many thanks

2 个评论

Before the loop:
n = 2; %whatever is appropriate
fmt = repmat('%f ', 1, n);
fmt(end:end+1) = '\n'; %newline, unrelated to the variable 'n'
In the loop:
[data, count] = sscanf(line, '%f');
if count >= n
fprintf(fid2, fmt, data(1:n));
end
Thanks a lot. I just tried but it doesn't work, it seems like it concatenates data in a single line.. Anyway many thanks

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by