negative numbers on table

조회 수: 6 (최근 30일)
Rui
Rui 2015년 2월 13일
댓글: Geoff Hayes 2015년 2월 14일
Hi there,
I have a cell with different types, for example cellN = {43.1; -29; 'xyz'}. Want to have a table with RowNames and VariableNames, and to do that, use cell2table.
When I do cell2table(cellN) I get the following 'Warning: Out of range or non-integer values truncated during conversion to character.'
After some search, I realised that is due to the negative number -29.
How to overcome this??
Thanks Rui

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 2월 13일
Rui - I suspect that the problem is more likely due to the fact that you have included a string, 'xyz', in with your numbers. In fact, if I remove the string from your cellN example and run the code
cell2table(cellN)
there is no error (like you, I observe the same warning when I leave the string in).
I think that the idea with the table is that all columns will be of a like data type, and you are mixing numbers with strings and so that can lead to unexpected behaviour (or warnings). If you wish to create a table from a cell array, then ensure that all elements in each column of the cell are of the same data type.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 2월 14일
Rui's answer moved here
Thanks Geoff.
But try keeping the string, and replacing -29 to 29. No error is issued. I guess the problem is not the different data types.
Geoff Hayes
Geoff Hayes 2015년 2월 14일
Rui - I see your point, that works too. The full warning message (for R2014a) is
Warning: Out of range or non-integer values truncated during conversion to character.
> In container2vars at 57
In cell2table at 35
If you put a breakpoint at line 57 of containers2vars.m, where the code is
for i = 1:size(cj,2), vars_j{i} = cat(1,cj{:,i}); end
and try to execute
cat(1,cj{:,i})
you will see the warning AND an exception will be thrown
Error using cat
Dimensions of matrices being concatenated are not consistent.
Note that this error is thrown for your example of just using the 43.1 with the string. (The code catches the error and hides it so you don't see this.)
During the concatenation, there seems to be some sort of conversion to a character array due to the presence of the string, 'xyz'. So something like
char(43.1)
"works" in that it produces a value of + (for whatever reason, try it and see), yet
char(-29)
gives the warning. If no string is present then neither the warning nor the exception is thrown.
This suggests that the presence of the string is the culprit and should be removed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by