Strcat multiple cell arrays not outputing expected output

5 次查看(过去 30 天)
Hello, below is the sample code that I am using to generate cell array of labels
labels = strcat(repmat({'sample'}, 100,1),{'_'}, num2cell([1:100]'));
And I expect to have 100x1 cell with following entries: sample_1; sample_2; sample_3; ... sample_99; sample_100;
However, when I execute the code in MATLAB R2015a, I get the following, which is very confusing...
>> strcat(repmat({'sample'}, 100,1),{'_'}, num2cell([1:100]'))
ans =
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample'
'sample_ '
[1x8 char]
'sample_'
'sample_'
[1x8 char]
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_'
'sample_ '
'sample_!'
'sample_"'
'sample_#'
'sample_$'
'sample_%'
'sample_&'
'sample_''
'sample_('
'sample_)'
'sample_*'
'sample_+'
'sample_,'
'sample_-'
'sample_.'
'sample_/'
'sample_0'
'sample_1'
'sample_2'
'sample_3'
'sample_4'
'sample_5'
'sample_6'
'sample_7'
'sample_8'
'sample_9'
'sample_:'
'sample_;'
'sample_<'
'sample_='
'sample_>'
'sample_?'
'sample_@'
'sample_A'
'sample_B'
'sample_C'
'sample_D'
'sample_E'
'sample_F'
'sample_G'
'sample_H'
'sample_I'
'sample_J'
'sample_K'
'sample_L'
'sample_M'
'sample_N'
'sample_O'
'sample_P'
'sample_Q'
'sample_R'
'sample_S'
'sample_T'
'sample_U'
'sample_V'
'sample_W'
'sample_X'
'sample_Y'
'sample_Z'
'sample_['
'sample_\'
'sample_]'
'sample_^'
'sample__'
'sample_`'
'sample_a'
'sample_b'
'sample_c'
'sample_d'
Could someone help explain this? I appreciate the help in advance!

采纳的回答

Stephen23
Stephen23 2016-6-14
编辑:Stephen23 2016-6-14
Explanation
strcat concatenates strings. However num2cell([1:100]') are not strings, they are numeric scalars in a cell array. Where is the string conversion? There isn't any! So strcat thinks that those numeric values are the characters' values, and returns the equivalent character corresponding to each value (some characters are not printable):
>> char(32:100)
ans = !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
Solution
To generate strings of those numeric values, try this:
tmp = arrayfun(@num2str,[1:100].','UniformOutput',false)
and also note that you do not need to use repmat or to put the strings 'sample' and '_' into cell arrays: this is a total waste of time and makes the intent of the code much harder to understand. Don't make code more complicated than it needs to be, just do this:
out = strcat('sample_',tmp)
Or you could get rid of the strcat altogether, and just do it using one arrayfun call:
arrayfun(@(n)sprintf('sample_%d',n), [1:100], 'UniformOutput',false)
  2 个评论
Louis
Louis 2016-6-14
编辑:Louis 2016-6-14
Thanks for the additional explanation..
Well, the original code looks like this, where the parts of each label rely on the for loop index
for i = 1:n
for j = 1:m
labels = [labels; strcat(repmat(strcat(dates(label1{i,2}{j,2}),{'_'},location(i)), 100, 1), {'_'}, arrayfun(@num2str, [1:100].','UniformOutput', false),'.png')];
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by