【问题标题】:Remove characters from a cell array of strings (Matlab)从字符串元胞数组中删除字符(Matlab)
【发布时间】:2013-03-14 20:44:03
【问题描述】:

我有一个字符串元胞数组。我需要为每个项目提取 1 到 n 个字符。字符串总是长于 n 个字符。请看:

data = { 'msft05/01/2010' ;
         'ap01/01/2013' }

% For each string, last 10 characters are removed and put it in the next column

answer = { 'msft' '05/01/2010' ;
           'ap'   '01/01/2013' }

是否有可能的矢量化解决方案?我曾尝试使用 cellfun,但没有成功。谢谢。

【问题讨论】:

    标签: string matlab character cell


    【解决方案1】:
    data = { 'msft05/01/2010' ;
             'ap01/01/2013' };
    for i = 1:length(data)
        s = data{i};
        data{i} = {s(1:end-10) s(end-9:end)};
    end
    

    抱歉,没有注意到您需要矢量化...也许我只能建议单行...

    data = cellfun(@(s) {s(1:end-10) s(end-9:end)}, data, 'UniformOutput', false);
    

    【讨论】:

    • 谢谢。然后用户可以通过 vertcat(data{:}) 得到最终的答案。
    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2011-03-14
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多