How to take a data form text file to put it in another file?

조회 수: 4 (최근 30일)
Yamina chbak
Yamina chbak 2022년 2월 3일
댓글: Yamina chbak 2022년 2월 4일

Hi,
I have a text file called File.1.node which contain such as:
45 12 7 1 ( first line )
1 2 1 1
2 3 2 1
3 4 5 1
.....
% File.1.node is complete (last line )

I want to take the data from File.1.node without first line and last line, to put it in another file named Node.dat.
But How i write a code for this problem?
Thanks in advance.

채택된 답변

Star Strider
Star Strider 2022년 2월 3일
Try this —
fidi = fopen('File.1.node','rt');
F1c = textscan(fidi, '%f%f%f%f', 'HeaderLines',1, 'CollectOutput',1);
fclose(fidi);
F1 = cell2mat(F1c);
dlmwrite(F1, 'Node.dat')
.
  댓글 수: 4
Star Strider
Star Strider 2022년 2월 4일
@Stephen — Thank you.
The other answer was originally accepted, and I deleted my original (correct) answer as the result. I got an e-mail requesting that I re-post it so that it could be accepted. I did it from memory, and did not remember the correct argument order to dlmwrite (correct in my original answer) because I rarely used it even before writematrix appeared. (My original answer used readmatrix and writematrix, however A Achbak does not have access to them. The dlmwrite call appeared in a subsequent Comment.)
Yamina chbak
Yamina chbak 2022년 2월 4일
Yes, I understand that now, well when I used dlmwrite (filename must be come first), it gives me a data but not form a matrix ( it was like : 1,2,1,1 2,3,2,1 .... ) So I prefer use @Stephen's code (removing the loop and the superfluous square brackets). Thanks you @Stephen.

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

추가 답변 (1개)

David Hill
David Hill 2022년 2월 3일
See what readmatrix gives you
m=readmatrix('File.1.node.txt');
m(1,:)=[];
writematrix(m,'Node.dat');
If you do not attach the file, we cannot test.
  댓글 수: 5
David Hill
David Hill 2022년 2월 3일
편집: David Hill 2022년 2월 3일
What about this?
m=dlmread('File.1.node',' ',[1 0 592 3]);
Yamina chbak
Yamina chbak 2022년 2월 3일
편집: Yamina chbak 2022년 2월 4일
Yes It's working now ! it can read the first raw. Thanks you so much @David Hill.
But, i just checked that it gives me a wrong data, so i just say myself maybe my text file is not organize to know where is the raw and the colone o the matrix.
Thanks you again @David Hill.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by