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

5 views (last 30 days)
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

Answers (1)

Star Strider
Star Strider on 17 Nov 2016
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 Comments
Ehsan Fatourehchi
Ehsan Fatourehchi on 17 Nov 2016
'CollectOutput' gives the same answer. Please find attached punch file.
Star Strider
Star Strider on 17 Nov 2016
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));

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!