Arrays 计算单元格数组中一行的字符数(包括空格)?

Arrays 计算单元格数组中一行的字符数(包括空格)?,arrays,string,matlab,character,cell,Arrays,String,Matlab,Character,Cell,我有一个单元格数组,每行都有字符串。我想计算单元格数组中每行的字符数,包括空格。如何在matlab中实现这一点 谢谢你的回答,我的代码有点卡住了 fid = fopen('thenames.txt', 'w'); if fid ~= -1 disp (' The file opened sucessfully') else disp (' The file did not open sucessfully') end string1 = input('Enter a name:

我有一个单元格数组,每行都有字符串。我想计算单元格数组中每行的字符数,包括空格。如何在matlab中实现这一点

谢谢你的回答,我的代码有点卡住了

fid = fopen('thenames.txt', 'w');
if fid ~= -1
    disp (' The file opened sucessfully')
else 
    disp (' The file did not open sucessfully')
end
string1 = input('Enter a name:','s');
countstr =1;
fprintf(fid, 'Name %d is %s\n', countstr, string1)
TF = strcmp ('stop', string1);
while TF == 0;
    countstr = countstr+1;
    string1 = input ('Enter a name:','s');
    TF = strcmp ('stop', string1);
    if TF ==0
fprintf(fid, 'Name %d is %s\n', countstr, string1)
    elseif TF == 1
            disp ('Ok. No more names')
    end

end
totalnames = countstr - 1;
fprintf(fid, 'There were %d total names given\n', totalnames)

fid = fopen('thenames.txt');
if fid ~= -1
disp (' The file opened sucessfully')
else 
disp (' The file did not open sucessfully')
end
cellarray2 = [];
cellarray2 = textscan (fid ,'%s %d %s %s');
newcellarray = {cellarray2{4}}
charsPerRow = sum(cellfun(@numel,newcellarray),2)

我最终尝试使用一个单元格数组,该数组将名称作为字符串存储在第一列中,第二列将字符串中的字符数作为整数存储。这将是我从第2行开始的最后一段代码,假设您有一个单元格数组
C

>> C = {'a','b2','c';'x','y','z';'aa','bb','cc'}
C = 
    'a'     'b2'    'c' 
    'x'     'y'     'z' 
    'aa'    'bb'    'cc'
然后使用
cellfun
sum

>> charsPerRow = sum(cellfun(@numel,C),2)
charsPerRow =
     4
     3
     6
假设要将其标记到单元格上:

>> C2 = [C num2cell(charsPerRow)]
C2 = 
    'a'     'b2'    'c'     [4]
    'x'     'y'     'z'     [3]
    'aa'    'bb'    'cc'    [6];

你能把
newcellarray
的内容吐出来吗?你应该做
newcellarray=Cellarray 2{4}
而不是
newcellarray={Cellarray 2{4}
,否则它是一个单元格数组中的单元格数组。然后您可以使用
cellfun
,如图所示。然后按照我更新的答案添加到包含字符计数的列中。没问题。如果我的评论和回答有帮助的话,如果你愿意的话,如果你愿意接受甚至投票,我将不胜感激。谢谢