How to delimit a punch file like Matlab (Import Data...) option?
조회 수: 12 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!