【问题标题】:How to make column cell-array from column struct array in Matlab?如何从 Matlab 中的列结构数组制作列单元格数组?
【发布时间】:2016-02-22 11:19:41
【问题描述】:

假设我得到目录列表

files = dir(mypattern);

现在我在 files 中有 10000x1 结构。

如果我这样做

filenames = {files.name};

我将在filenames 中有 1x10000 个单元格。

即换位。

如何一步一步从files 获取 10000x1 单元格?

【问题讨论】:

  • 为什么会有这么大的问题?

标签: matlab struct cell-array


【解决方案1】:

怎么样

filenames = {files.name}.';

【讨论】:

    【解决方案2】:

    只是为了解释正在发生的事情。 files.name 是一个 Comma-Separated List,其行为类似于 {files(1).name,files(2).name,files(3).name}(简化为三个元素),因为它由逗号分隔,而不是由分号分隔。没有分号分隔列表或类似列表,您必须转置或重塑才能获得所需的尺寸。

    对于向量,来自 Shai 的解决方案是完美的,reshape 是一个简单的解决方案,也适用于更多维数据结构:

    files=struct('names',{'a','b';'c','d'}); %example data 2x2
    reshape({files.names},size(files));
    

    【讨论】:

      【解决方案3】:

      另一种方法是:

      filenames = arrayfun(@(x) x.name, dir(mypattern), 'uni',0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多