Search code examples
matlabfor-looppoint-clouds

Reading files frame by frame


I have a folder containing .ply files. I want to read them and plot them like an animation. Initially i am trying to read the files and plot individually using the following code:

testfiledir = 'Files\';
plyfiles = dir(fullfile(testfiledir, '*.ply'));

for k=1:length(plyfiles)
   FileNames = plyfiles(k).name;
   plys=pcread(FileNames);
   pcshow(plys)
end

But while running the script i get the error: Error using pcread (line 51) File "val0.ply" does not exist. Error in read_pcd (line 6) plys=pcread(FileNames);

val0.ply is one my first frame which is read in the variable 'plyfiles'

Where am I making mistake?


Solution

  • Use a datastore it is much easier and will keep track of everything for you. E.g.

    ds = fileDatastore("Files/","ReadFcn",@pcread,"FileExtensions",".ply");
    

    then you can read the files from it using read or readall, e.g.

    while hasdata(ds)
        plys = read(ds);
        pcshow(plys)
    end
    

    It is slightly slower than if you can make the optimal implementation, but I prefer it big-time for its ease.