Read a punch file
조회 수: 41 (최근 30일)
이전 댓글 표시
Hi everyone,
I need to read a punch file (.pch). And then I have to create a matrix from the values contained in this file.
I am having some problem on reading this file on Matlab.
Here you can find attached a screenshot from the file. (the .pch file is to large to attach)
So I basically want to get the first coloumn of integers (10,11.........) and the six floatings with a for cycle but I don't know how to read the file.
I would be glad if anyone helps me. Thank you in advance.
댓글 수: 4
답변 (1개)
Voss
2022년 9월 19일
This may work:
% read the file contents as a char vector:
fid = fopen('sample.txt'); % (had to change extension to txt to upload)
str = char(fread(fid).');
fclose(fid);
% split into a cell array of char vectors, one cell per line:
lines = strtrim(split(str,newline()));
% remove lines starting with '$':
lines(startsWith(lines,'$')) = [];
% coombine -CONT- lines with the previous non--CONT- line:
is_start_line = ~startsWith(lines,'-CONT-');
idx = cumsum(is_start_line);
new_lines = cell(idx(end),1);
for ii = 1:numel(lines)
if is_start_line(ii)
new_lines{idx(ii)} = lines{ii};
else
new_lines{idx(ii)} = [new_lines{idx(ii)} lines{ii}(7:end)];
end
end
% sscanf each line to get the numbers out:
data = zeros(numel(new_lines),8);
for ii = 1:numel(new_lines)
data(ii,:) = sscanf(new_lines{ii},'%d%s%g%g%g%g%g%g');
end
% remove the character ('G') column:
data(:,2) = [];
% check the result:
format longG
disp(data)
댓글 수: 1
Rodrigo Martín Ortiz
2022년 10월 3일
Hello, I believe I have a very similar task in my hands to the original poster.
Firstly, your code has been very helpful in order to understand the process, since I've never had to deal with reading such kinds of files yet.
However, when running (I got the same NASTRAN .pch file, which I attach) I obtain a dimensions error on this line:
data(ii,:) = sscanf(new_lines{ii},'%d%s%g%g%g%g%g%g');
Ive tried removing the CONT line completely, since I dont need it for my application. However, when "sending" the new_lines cell array to the data matrix, I get these weird dimensions error. I think I may not be understanding the second part of the sscanf function.
Since the original poster did not attach the file, which I imagine must be very useful in order to get an accurate answer, I will do it, although in a txt. Hope it helps.
Help would be very much appreciated. Thank you.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!