首页 > 解决方案 > regexp does not work as expected in Octave

问题描述

I have downloaded the "NYU Depth V2" dataset and toolbox from here. In the toolbox there is a script called get_synched_frames.m. I do not have Matlab, so I have tried running it in Octave. Unfortunately, it does not work as expected.

The line

% Faster than matlab's Dir function for big directories and slow
% distributed file systems...
files = regexp(ls(sceneDir), '(\s+|\n)', 'split');

gives only

files =
{
  [1,1] = a-1300302776.479149-3987628315.dump
}

but ls(sceneDir) shows all files in the directory. Has anyone experienced this?

标签: octave

解决方案


The difference is probably not in regexp, but in the return value of ls. ls does not behave the same way in Matlab and Octave when you capture its return value. Matlab's ls returns a char row vector (single string as char) with multiple files listed in it as a multi-line string with embedded newlines; Octave's ls returns a 2-D char array with one file per line. (IMHO Octave's format is better; it is very difficult to parse Matlab's ls output in a reliably correct manner. (That regexp code is not adequate.))

You might just want this in Octave:

files = cellstr(ls(sceneDir));

推荐阅读