使用 Octave 将目录中的文件自然排序到元胞数组中
我有由另一个程序/用户生成的文件,其名称例如“jh-1.txt,jh-2.txt,...,jh-100.txt,...,jh-1024.txt”。我从这些文件中提取一列,操作数据,然后输出到一个新矩阵。唯一的问题是 Octave 在读取文件时使用 ASCII 排序而不是自然排序。因此,输出矩阵不是以自然方式排序的。我的问题是,Octave 可以按自然顺序对文件名进行排序吗?我以标准方法获取文件名:
fileDirectory = '/path/to/directory';
filePattern = fullfile(fileDirectory, '*.txt'); % Selects only the txt files.
dataFiles = dir(filePattern); % Gets the info from the txt files in the directory.
baseFileName = {dataFiles.name}'; % Gets all the txt file names.
我无法重命名文件,因为这是另一个用户的脚本。他们在 Windows 计算机上,并且已经安装了 Octave 和 Cygwin,我不想让他们过多地使用命令行,因为他们不熟悉命令行。或者,最好将输出与文件名放在一列中,但是我也没有想到这一点(我自己对 Octave 有点菜鸟)。这样用户就可以使用 Excel(他们熟悉的)对列进行排序。
I have files being generated by another program/user that have names such as "jh-1.txt, jh-2.txt, ..., jh-100.txt, ..., jh-1024.txt". I'm extracting a column from these files, manipulating the data, and outputting to a new matrix. The only problem is that Octave is using ASCII ordering and not natural ordering when reading in the files. Thus, the output matrix is not ordered in a natural way. My question is, can Octave sort file names in a natural order? I'm getting file names in the standard method:
fileDirectory = '/path/to/directory';
filePattern = fullfile(fileDirectory, '*.txt'); % Selects only the txt files.
dataFiles = dir(filePattern); % Gets the info from the txt files in the directory.
baseFileName = {dataFiles.name}'; % Gets all the txt file names.
I can't rename the files because this is a script for another user. They are on a Windows machine and already have Octave installed with Cygwin and I don't want to make them use the command line more than they have to because they are unfamiliar with it. Alternatively, it would be nice to have the output with the file names in a column but, I haven't figured that one out either (bit of a noob with Octave myself). That way the user could use Excel (which they are familiar with) to sort the columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为 Octave 中有内置的自然排序。但是,Mathwork 的文件交换上有自然排序提交。我没有使用过它,但评论暗示它也可以在 Octave 中使用。
I don't think there's a built in natural sort in Octave. However, there is a natural sort submission on Mathwork's File Exchange. I've not used it, but the comments imply it works in Octave too.