Strcat multiple cell arrays not outputing expected output

5 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Stephen23
Stephen23 il 14 Giu 2016
Modificato: Stephen23 il 14 Giu 2016
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 Commenti
Louis
Louis il 14 Giu 2016
Modificato: Louis il 14 Giu 2016
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by