Difference between num2cell(1:4)' and {'1','2','3','4'}' ?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Laurent Davenne
il 21 Nov 2017
Commentato: Guillaume
il 22 Nov 2017
Hi,
I am having an error on creating a table with 'RowNames' property.
truc=array2table([-0.013 -0.470 0.109
-0.013 -0.454 0.107
-0.012 -0.437 0.104
0.009 0.077 -0.088],'VariableNames',{'Scx','SczF','SczR'},'RowNames',num2cell(1:4)');
Main error is: The RowNames property must be a cell array, with each element containing one nonempty character vector.
>> num2cell([1 2 3 4])'
ans =
4×1 cell array
[1]
[2]
[3]
[4]
>> {'1','2','3','4'}'
deux =
4×1 cell array
'1'
'2'
'3'
'4'
In both case it is a cell array, but the content is different.
3 Commenti
John D'Errico
il 21 Nov 2017
Is it true that the number 1 is equal to the character string that contains the character '1'?
Of course not. So why are you surprised they are not the same?
Risposta accettata
Guillaume
il 21 Nov 2017
The simplest way to convert numbers to strings or char arrays since R2016b is:
string(1:15) %that's it!
In earlier versions:
sprintfc('%d', 1:15) %undocumented function!
arrayfun(@num2str, 1:15, 'UniformOutput', false) %using only documented functions
2 Commenti
Guillaume
il 22 Nov 2017
They all work! The last two in any version.
You can pass a format string to num2str:
arrayfun(@(n) num2str(n, '%.1f'), 3:0.5:34, 'UniformOutput', false)
Più risposte (1)
dpb
il 21 Nov 2017
Modificato: dpb
il 21 Nov 2017
As the error message says, the cell content must be character string and Adam notes the difference between the two forms you wrote; the first is numeric values placed into a cell array, the second is the string value but you wrote manually..
To build the equivalent cellstr array for the purpose use
> cellstr(num2str([1:3].','%d'))
ans =
'1'
'2'
'3'
>>
or similar.
However I'd note there's not a lot of point in creating row names that are just ordinal values in sequence as you can address rows numerical simply by using the row number in array expression syntax.
ERRATUM Corrected the misplaced transpose operator discussed in following comments -- dpb
3 Commenti
Guillaume
il 21 Nov 2017
Humm....
Format is messed up when numbers go over 10
>> cellstr(num2str(1:14,'%d')')
ans =
27×1 cell array
'1'
''
'2'
''
'3'
''
'4'
''
'5'
''
'6'
''
'7'
''
'8'
''
'9'
'1'
'0'
'1'
'1'
'1'
'2'
'1'
'3'
'1'
'4'
in fact I even need it to work for decimals
cellstr(num2str(3:0.5:34,'%d')')
I am quite lost with format spec... :(
dpb
il 21 Nov 2017
Modificato: dpb
il 21 Nov 2017
Misplaced location of the .' transpose operator to build the column vector, my bad. At the trailing location it takes the horizontal row vector of characters
>> num2str(1:14,'%d')
ans =
1 2 3 4 5 6 7 8 91011121314
>>
and transposes it to a column vector; hence the blank lines. What is needed and was intended is to transpose the numeric vector to column form, then convert it--
>> num2str([1:14].','%2d')
ans =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
>> cellstr(num2str([1:14].','%2d'))
ans =
' 1'
' 2'
' 3'
' 4'
' 5'
' 6'
' 7'
' 8'
' 9'
'10'
'11'
'12'
'13'
'14'
>>
There will be some purists who'll nag me about using the [] where only () are needed, and it is true in a very deeply nested loop you might see a little performance hit, but I simply like the visual representation sufficiently to accept the hit.
Vedere anche
Categorie
Scopri di più su Data Type Conversion 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!