필터 지우기
필터 지우기

How to delimit a punch file like Matlab (Import Data...) option?

조회 수: 3 (최근 30일)
Ehsan Fatourehchi
Ehsan Fatourehchi 2016년 11월 17일
댓글: Star Strider 2016년 11월 17일
Hi all,
I need to open a punch file (ASCII format file) in Matlab and change some values on that. From the following function to open and save the file in Matlab workspace. The problem is this function save the file in nx1 matrix. Does anyone know how can I import the punch file in a way that it splits spaces and commas to cells? Like what Matlab does using Import data option.
fid = fopen('tm2.pch','r');
MyText = textscan(fid,'%s ','delimiter','\n');
fclose(fid);
MyText = [MyText{:}];
For example, how to save the following data (a section of punch file) from text file to a 3x3 matrix?
MAT4 1001 100.
MAT4 1002 200.
MAT4 1003 300.
Thanks a lot

답변 (1개)

Star Strider
Star Strider 2016년 11월 17일
We need you to attach ‘tm2.pch’ to provide specific code.
Knowing only what you’ve posted, adding 'CollectOutput' could do what you want:
MyText = textscan(fid,'%s ','delimiter','\n', 'CollectOutput',true);
  댓글 수: 2
Ehsan Fatourehchi
Ehsan Fatourehchi 2016년 11월 17일
'CollectOutput' gives the same answer. Please find attached punch file.
Star Strider
Star Strider 2016년 11월 17일
I couldn’t find ‘MAT4’ anywhere in your file. I even opened it and looked at in ‘notepad’.
This code works to read it, and if ‘MAT4’ existed, a bit more code would produce the matrix you want. Since it doesn’t contain any ‘MAT4’ entries, I can’t write specific code to create your matrix.
The Code
fid = fopen('tm2.pch','r');
MyText = textscan(fid,'%s%f%f%f','Delimiter','\t', 'HeaderLines',7, 'CollectOutput',true);
idx1 = strfind(MyText{1},'MAT4');
MAT4_rows = find(cell2mat(idx1));

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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by