string matlab如何将单元格转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13258508/
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
how to convert a cell to string in matlab
提问by user1804229
Suppose I have a cell
假设我有一个细胞
v = 'v' [576.5818] [3.0286] [576.9270]
'v' [576.5953] [3.1180] [576.8716]
'f' [ 56] [ 58] [ 52]
'f' [ 56] [ 58] [ 52]
and I want to convert this to a cell array using a format string for each element:' %.5f'
我想使用每个元素的格式字符串将其转换为元胞数组:' %.5f'
How can I do this? I tried the following approach, but I get an error:
我怎样才能做到这一点?我尝试了以下方法,但出现错误:
f1 = @(x) sprintf(' %.5f',x);
cellfun(f1, num2cell(v),'UniformOutput', false)
I am getting an error as ???
我收到一个错误????
Error using ==> sprintf
Function is not defined for 'cell' inputs.
Error in ==> @(x)sprintf(' %.5f',x)
Error using ==> sprintf
Function is not defined for 'cell' inputs.
Error in ==> @(x)sprintf(' %.5f',x)
Can any one help me thanks in advance
任何人都可以帮助我在此先感谢
回答by Yauhen Yakimovich
String is a cell array
字符串是一个元胞数组
Well, not really.. It is a matrix, but continue reading.
嗯,不是真的......它是一个矩阵,但继续阅读。
I guess cell array is the most mystic data type in MATLAB. So let's demystifyit a bit ;-)
我猜元胞数组是 MATLAB 中最神秘的数据类型。所以让我们揭开它的神秘面纱;-)
Assume
认为
fruits = {...
'banana',...
'apple',...
'orange'...
}
First of all integer indexing is not needed for small arrays. It is much better to use foreach-like constructions. Indeed,
首先,小数组不需要整数索引。使用类似 foreach的结构要好得多。的确,
for index = 1:numel(fruits)
fruits{index}
end
is equivalent to
相当于
for fruit = fruits
fruit
end
right?
对?
Well, not quite. First loop produces strings, while the second one gives cells. You can check it with
嗯,不完全是。第一个循环产生字符串,而第二个循环产生单元格。你可以检查它
for index = 1:numel(fruits)
[isstr(fruits{index}) iscell(fruits{index})]
end
for fruit = fruits
[isstr(fruit) iscell(fruit)]
end
, i.e. [1 0]and [0 1].
,即[1 0]和[0 1]。
If you have spot the difference, then you must know what to do with the next example (in this one is really relate to your question (!)I promise!). Say you try to do horizontal concatenation in a loop:
如果您发现了差异,那么您必须知道如何处理下一个示例(在这个示例中确实与您的问题有关(!)我保证!)。假设您尝试在循环中进行水平连接:
for fruit = fruits
[fruit 'is a fruit']
end
You will get
你会得到
ans =
'banana' 'is a fruit'
and so on. Why? Apparently this code tries to concatenate a nested cell array to a string (a cell array containing a matrix of chars which constitute the string like 'banana'). So, correct answer is
等等。为什么?显然,这段代码试图将嵌套的元胞数组连接到一个字符串(一个元胞数组,该元胞数组包含构成像“香蕉”这样的字符串的字符矩阵)。所以,正确答案是
Use {:}
用 {:}
for fruit = fruits
[fruit{:} 'is a fruit']
end
Magically this already produces the expected 'banana is a fruit', 'apple is a fruit', etc.
神奇的是,这已经产生了预期的“香蕉是水果”,“苹果是水果”等。
Hints
提示
A few hints:
一些提示:
- Index-free looping works nicely with structs as in
for fruit = [fieldnames][1](fruits)'
- The above is truefor open source octave
- banana is not just fruit, taxonomically it is also a herb ;-) just like 'banana' in MATLAB is both a string and a matrix, i.e. assert(isstr('banana') && ismat('banana')) passes, but assert(iscell('banana')) fails.
{:}
is equivalent tocell2mat
- 无索引循环与结构很好地配合,如
for fruit = [fieldnames][1](fruits)'
- 以上适用于开源八度音阶
- 香蕉不仅仅是水果,在分类学上它也是一种草本植物 ;-) 就像 MATLAB 中的 'banana' 既是字符串又是矩阵,即 assert(isstr('banana') && ismat('banana')) 通过,但是assert(iscell('banana')) 失败。
{:}
相当于cell2mat
PS
聚苯乙烯
a solution to your question may look like this:
您的问题的解决方案可能如下所示:
Given
给定的
vcell = {...
'v' 576.5818 3.0286 576.9270;
'v' 576.5818 3.0286 576.9270
}
covert index-wise only numeric types to strings
将仅按索引的数字类型转换为字符串
vcell(cellfun(@isnumeric, vcell)) = cellfun(@(x) sprintf('%.5f', x), vcell(cellfun(@isnumeric, vcell)), 'UniformOutput', false)
Above code outputs
以上代码输出
vcell =
vcell =
'v' '576.58180' '3.02860' '576.92700'
'v' '576.58180' '3.02860' '576.92700'
which can be concatenated.
可以连接。
回答by merlin2011
Suppose we have a cell as follows:
假设我们有一个如下的单元格:
my_cell = {'Hello World'}
class(my_cell)
ans =
cell
We can get the string out of it simply by using the {:}
operator on it directly.
我们可以通过直接在字符串上使用{:}
运算符来获取字符串。
class(my_cell{:})
ans =
char
Note that we can use the expression mycell{:}
anywhere we would use a normal string.
请注意,我们可以在mycell{:}
使用普通字符串的任何地方使用该表达式。
回答by Dieter
By looking in the strjoin.m file i found this:
通过查看 strjoin.m 文件,我发现了这一点:
string = [x{:}];