string MATLAB 空单元格(n,m)字符串数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8895575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:21:12  来源:igfitidea点击:

MATLAB empty cell(n,m) array of strings?

stringmatlabcell

提问by HeinrichStack

What is the quickest way to create an empty cell array of strings ?

创建空的字符串元胞数组的最快方法是什么?

cell(n,m)

creates an empty cell array of double.

创建一个空的 double 元胞数组。

How about a similar command but creating empty strings ?

一个类似的命令,但创建空字符串怎么样?

回答by John

Depends on what you want to achieve really. I guess the simplest method would be:

取决于你真正想要达到的目标。我想最简单的方法是:

repmat({''},n,m);

回答by Kavka

Assignment to all cell elements using the colon operator will do the job:

使用冒号运算符分配给所有单元格元素将完成这项工作:

m = 3; n = 5;
C = cell(m,n);
C(:) = {''}

回答by zeFrenchy

The cell array created by cell(n,m) contains empty matrices, not doubles. If you really need to pre populate your cell array with empty strings

cell(n,m) 创建的元胞数组包含空矩阵,而不是双精度矩阵。如果你真的需要用空字符串预先填充你的元胞数组

test = cell(n,m);
test(:) = {''};
test(1,:) = {'1st row'};
test(:,1) = {'1st col'};

回答by Y. Chang

This is a super old post but I'd like to add an approach that might be working. I am not sure if it's working in an earlier version of MATLAB. I tried in 2018+ versions and it works. Instead of using remat, it seems even more convenient and intuitive to start a cell string array like this:

这是一篇非常老的帖子,但我想添加一种可能有效的方法。我不确定它是否在早期版本的 MATLAB 中工作。我尝试了 2018+ 版本,并且有效。与使用 相比remat,像这样启动一个元胞字符串数组似乎更加方便和直观:

C(1:10) = {''} % Array of empty char

And the same approach can be used to generate cell array with other data types

并且可以使用相同的方法生成具有其他数据类型的元胞数组

C(1:10) = {""} % Array of empty string
C(1:10) = {[]} % Array of empty double, same as cell(1,10)

But be careful with scalers

但要小心缩放器

C(1:10) = {1} % an 1x10 cell with all values = {[1]}
C(1:10) = 1 % !!!Error
C(1:10) = '1' % !!!Error
C(1:10) = [] % an 1x0 empty cell array