问题
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:
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.
来源:https://stackoverflow.com/questions/33451725/reading-a-ply-file-from-a-specific-string