【问题标题】:Matlab: trasform to cell of stringsMatlab:转换为字符串单元格
【发布时间】:2015-12-03 08:29:17
【问题描述】:

问题:我有包含 char 单元格的后缀 (137x25) 单元格(Ex postfix(6,8)=postfix{6,8}{1,1})。我想在字符的 (137x25) 单元格中对其进行转换。

后缀是这样创建的:

for l=1:25
    [matches(:,l), postfix(:,l)] = regexp(semanticTrajCompact(1,4).TrajCompact,sprintf('%d%d(.*)',digits{1}(l),digits{2}(l)),'match','once','tokens');
end

我尝试了不同的解决方案:

解决方案 1

numIndex = cellfun('isclass', postfix, 'double');
  tmpStr = sprintf('%g;', postfix{numIndex});
  postfix(numIndex) = dataread('string', tmpStr, '%s', 'delimiter', ';');

解决方案 2

postfix(cellfun(@isempty,postfix))={''};

解决方案 3

postfix(cellfun(@isnumeric, postfix)) =  cellfun(@(x) sprintf('%.5f', x), postfix(cellfun(@isnumeric, postfix)), 'UniformOutput', false)

解决方案 4

我尝试使用

char(postix)

此解决方案中的任何一个都将后缀转换为(137x25 字符数组)。你能给我其他的想法吗?

【问题讨论】:

  • 您能否举一个小例子(使用类似 2 x 2 单元格数组)说明原始单元格的外观以及您希望得到的结果?

标签: string matlab cell


【解决方案1】:

您希望循环遍历元胞数组后缀的每个元素,并将元胞数组(本身就是元胞数组)的每个元素替换为其内容。使用cellfun 替换循环,因此您编写:

postfix = cellfun(@(x)x{1}, postfix, 'UniformOutput', false);

【讨论】:

    【解决方案2】:

    我也是这样解决问题的

    for k=1:25
        for j=size(postfix,1)
            if(~isempty(postfix{j,k}))
                postfix{j,k}=char(postfix{j,k}{1,1});
            end
        end
    end
    
    postfix(cellfun(@isempty,postfix))={''};
    

    【讨论】:

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