Reading a PLY file from a specific string

2019-06-09 21:09发布

I want to read a PLY file to a MATLAB matrix starting from the next line of the string end_header using the dlmread function as suggested in this SOF question. Sample PLY file is given here. Currently the starting line is hardcoded as follows, but it is not suitable as the number of header rows in a PLY file may change.

data = dlmread(fileName, ' ', 14, 0);

1条回答
啃猪蹄的小仙女
2楼-- · 2019-06-09 21:44

There are a multitude off different ways you could do this. One option is to use textscan to read in the entire file and a mix of strfind and find to determine the row that contains 'end_header' like

filename = 'testPly.ply';

fid = fopen(filename);
data = textscan(fid, '%s', 'delimiter', '\n');
idx = find(cellfun(@isempty, strfind(data{1}, 'end_header')) == 0);
fclose(fid);

then you can use dlmread as

data = dlmread(filename, ' ', idx, 0);

or extract the numeric data based on my previous answer.


Another method, which may be better if your files contain a lot of data after 'end_header' but not a lot before it would be to read each line until you find 'end_header' using fgets

idx = 1;
fid = fopen(filename, 'r');
while isempty(strfind(fgets(fid), 'end_header'))
    idx = idx + 1;
end
fclose(fid);

and then use dlmread or extract the numeric data based on my previous answer.

查看更多
登录 后发表回答