필터 지우기
필터 지우기

Text File read and write out

조회 수: 49 (최근 30일)
Thuan
Thuan 2017년 5월 24일
댓글: Walter Roberson 2017년 5월 25일
Hey guys, I'm new to Matlab, and hoping I can get some some help. I'm trying to read in a text file and write it to the format I want. fileread('file') will give me the following data
1356297 2 0.0521214521 0.0005944170 0.0016546078 0.0125977102
4197358 89 0.0523557459 0.1244454656 -0.164303258 -0.542853886
3679865 104 0.0540524244 -0.015693102 -0.004209815 0.0009847531
I want it to write this data to the following format.
"Node 1356297", "Node 4197358", "Node 3679865"
"<0.0005944170, 0.0016546078, 0.0125977102>", @
"<0.1244454656, -0.164303258, -0.542853886>", @
"<-0.015693102, -0.004209815, 0.0009847531>", @
Thanks, Thuan
  댓글 수: 2
Fernando Fernandes
Fernando Fernandes 2017년 5월 24일
I think you may find your answer here:
https://www.mathworks.com/help/matlab/ref/textread.html
Walter Roberson
Walter Roberson 2017년 5월 24일
textread is not recommended; it has been pretty much replaced by textscan or other routines.

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 5월 24일
With your regular structure and no headers, you can use load() to read the file.
data = load('file');
ids = data(:,1);
vecs = data(:,4:end);
fid = fopen('Output.txt', 'wt');
fmt = [strjoin( repmat( {'"Node %d"'}, 1, length(ids), ', '), '\n'];
fprintf(fmt, ids);
fmt = ['"<', strjoin( repmat( {'%f'}, 1, size(vecs,2) ), ','), '>" @\n'];
fprintf(fmt, vecs.'); %transpose is important
fclose(fid);
  댓글 수: 2
Thuan
Thuan 2017년 5월 25일
편집: Walter Roberson 2017년 5월 25일
How would I do it if I have a heading let say
MSC.Patran 21.1.348049 Fri Jul 03 23:12:17 PDT 2015 - Analysis Code: MSC.Nastran
Load Case: 653215
Result
1356297 2 0.0521214521 0.0005944170 0.0016546078 0.0125977102
Walter Roberson
Walter Roberson 2017년 5월 25일
If I correctly count there as being 4 header lines, then,
fid = fopen('file', 't');
data = cell2mat( textscan(fid, '', 'HeaderLines', 4, 'CollectOutput', true) );
fclose(fid);

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

카테고리

Help CenterFile 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