Filter löschen
Filter löschen

Difference between num2cell(1:4)' and {'1','2','3','4'}' ?

1 Ansicht (letzte 30 Tage)
Laurent Davenne
Laurent Davenne am 21 Nov. 2017
Kommentiert: Guillaume am 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 Kommentare
Stephen23
Stephen23 am 21 Nov. 2017
Bearbeitet: Stephen23 am 21 Nov. 2017
"In both case it is a cell array, but the content is different."
Yes. You just answered your own question: the content is indeed different. You put scalar numeric into one, and chars into the other.
John D'Errico
John D'Errico am 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 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 Kommentare
Laurent Davenne
Laurent Davenne am 21 Nov. 2017
Thanks Guillaume,
last one works.
>> (arrayfun(@num2str, 3:0.5:34, 'UniformOutput', false))'
ans =
63×1 cell array
'3'
'3.5'
'4'
'4.5'
'5'
'5.5'...
But what if I need it to be with 1 decimal everywhere?
Guillaume
Guillaume am 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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 21 Nov. 2017
Bearbeitet: dpb am 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 Kommentare
Guillaume
Guillaume am 21 Nov. 2017
Comment by Laurent Davenne mistakenly posted as an answer:
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
dpb am 21 Nov. 2017
Bearbeitet: dpb am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by