如何在matlab中删除开头和结尾的空格?

ber*_*ndh 5 matlab cell removing-whitespace

我有一个由细胞组成的结构.我想删除每个单元格的开头和最后的所有空格,我想保留单元格中文本之间的所有空格.所以,如果我有

s = '   bbb b bbbb   ' 
Run Code Online (Sandbox Code Playgroud)

我想获得

s = 'bbb b bbbb' 
Run Code Online (Sandbox Code Playgroud)

我想将此方法应用于此结构中的未知数量的单元格(例如2x3),可能使用循环.有谁知道怎么做?我失败了regexp.

Rod*_*uis 5

您可以strtrim()结合使用structfun()和细胞索引:

your_struct = structfun(@(x) strtrim(x{1}), your_struct);
Run Code Online (Sandbox Code Playgroud)

这仅适用于您的结构具有类似布局的情况

your_struct.a = {' some string  '};
your_struct.b = {' some other string  '};
...
Run Code Online (Sandbox Code Playgroud)

如果它有不同的结构,比方说,

your_struct.a = { {' some string  '}
                  {'   some other string '}};

your_struct.b = { {' again, some string  '}
                  {'   again, some other string '}};

...
Run Code Online (Sandbox Code Playgroud)

你可以试试

your_struct = structfun(@(x) ...
    cellfun(@strtrim, x, 'uni', false), ...
    your_struct, 'uni', false);
Run Code Online (Sandbox Code Playgroud)