string 创建字符串的元胞数组 matlab

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19167843/
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 02:06:37  来源:igfitidea点击:

create a cell array of strings matlab

stringmatlabcell

提问by brucezepplin

Hi I am trying to create a cell array of strings with:

嗨,我正在尝试使用以下命令创建一个字符串元胞数组:

data = ['1';'2';'3';'4';'5';'6';'7';'8';'9';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';];

where I expecting a cell array of 25 elements. but I get:

我期望一个包含 25 个元素的单元格数组。但我得到:

length(data)

= 33

so obviously numbers 12,13 etc are counting as 2 bits.

所以显然数字 12,13 等算作 2 位。

My question is then how do I ensure the cell array is of length 20? also the function I am putting the cell array into has to be a cell array of strings even though I am using ints!

我的问题是如何确保单元格数组的长度为 20?即使我使用的是整数,我将元胞数组放入的函数也必须是字符串元胞数组!

回答by Justin

You need to do:

你需要做:

data = {'1';'2';'3';'4';'5';'6';'7';'8';'9';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';};

Use {}. These will form a cell array.

使用{}. 这些将形成一个单元阵列。

回答by chappjc

You can use {}instead of []to build a cell, or you can use strsplitto build an arbitrary length cell of strings representing numbers from 1 to N:

您可以使用{}代替[]来构建一个单元格,或者您可以使用strsplit来构建一个任意长度的字符串单元格,该字符串表示从 1 到 的数字N

data = strsplit(num2str(1:N));

Update:The fastest way to do this now is with the undocumented sprintfcfunction (note the "c" at the end) which prints each element to it's own cell:

更新:现在最快的方法是使用未记录的sprintfc函数(注意末尾的“c”),它将每个元素打印到它自己的单元格:

>> A = sprintfc('%g',1:20)
A = 
  Columns 1 through 11
    '1'    '2'    '3'    '4'    '5'    '6'    '7'    '8'    '9'    '10'    '11'
  Columns 12 through 20
    '12'    '13'    '14'    '15'    '16'    '17'    '18'    '19'    '20'
>> which sprintfc
built-in (undocumented)