Hello,
Basically, I have a .txt file in which I have a first string of characters, a second string of characters divided in 5 columns and finally the following lines are divided in 5 columns as well and filled with numbers.
As a scheme explains better than my bad english, the layout is the following one :
Nodal displacements (DX,DY,DZ) [Code 163] (Vibration Mode Number)
Node Component X Component Y Component Z Modulus
11424 1 -0.1784254 -0.14097163 1.02552846
11428 0.99940151 -0.17841142 -0.14048249 1.02487528
11427 0.99868083 -0.17837693 -0.13998173 1.02409795
11426 0.99781752 -0.17834243 -0.13945551 1.02317821
11425 0.99682903 -0.17832828 -0.13891853 1.02213867
11423 0.97537661 -0.17893241 -0.14097127 1.0016233
11437 0.97482145 -0.17892055 -0.14049338 1.00101339
...
I'd like to develop a routine that reads and writes the .txt file to delete the two first lines of characters, i.e.,
Nodal displacements (DX,DY,DZ) [Code 163] (Vibration Mode Number)
Node Component X Component Y Component Z Modulus ,
to be able to have a 5-columns array with nothing but only numbers that I can load in Matlab...
Thanks in advance. O.

 채택된 답변

Cedric
Cedric 2013년 4월 14일
편집: Cedric 2013년 4월 14일

7 개 추천

If you read the file using TEXTREAD, you don't need to have the file truncated first, because you can tell TEXTREAD to skip a given number of header lines. Look at the doc for this function and the parameter named 'headerlines'. Example:
[node, X, Y, Z, modulus] = textread('myFile.txt', '%f %f %f %f %f', ...
'headerlines', 2) ;
If you really want to remove 2 lines from a file, you can build a solution around:
fid = fopen('myFile.txt', 'r') ; % Open source file.
fgetl(fid) ; % Read/discard line.
fgetl(fid) ; % Read/discard line.
buffer = fread(fid, Inf) ; % Read rest of the file.
fclose(fid)
fid = fopen('myFile_truncated.txt', 'w') ; % Open destination file.
fwrite(fid, buffer) ; % Save to file.
fclose(fid) ;

댓글 수: 4

Olivier
Olivier 2013년 4월 16일
It worked great! Thank you.
Ahmad Vasel
Ahmad Vasel 2016년 4월 18일
Very smart, the only problem is that it does not remove empty spaces below header. Can you assist me with this issue? I need to remove empty spaces too. Thanks.
Anna
Anna 2016년 7월 31일
How do I repeat this for every .txt file in the folder without manually typing the names in?

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

추가 답변 (2개)

Tobias
Tobias 2013년 4월 14일
편집: Tobias 2013년 4월 14일

0 개 추천

I have been given the following code by a teacher at university:
function allData =ReadVerTxtData(filename,NumVar)
fid=fopen(filename,'r');
tline1 = fgetl(fid); %%Vernier Format 2
tline2 = fgetl(fid); %Untitled.cmbl 4/7/2008 20:19:02 .
tline3 = fgetl(fid); %Latest
tline4 = fgetl(fid); %Time Potential
tline5 = fgetl(fid); %t Pot
tline6 = fgetl(fid); %s V
tline7 = fgetl(fid); %
tline8 = fgetl(fid); %0 7.60683760684
allData=fscanf(fid,'%f',[NumVar,Inf]); %
fclose(fid);
It reads data from a text file and ignores the first 8 lines. You could add and delete lines as you desire.
Edit: I should probably add that the two inputs of the function is: your file directory + name AND the number variables / lines you want printed into matlab, 5 in your case.

댓글 수: 2

Olivier
Olivier 2013년 4월 16일
Thank you for your answer :)
Simon Kumm
Simon Kumm 2016년 7월 19일
Note your "allData" dimensions. Maybe you got to transpose!

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2013년 4월 14일

답변:

2017년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by