table dispaly with char array

조회 수: 6 (최근 30일)
yudi vass
yudi vass 2016년 10월 21일
댓글: yudi vass 2016년 10월 27일
Got the below code, not sure why the table display of the char array is not right
Tval = table(name, x_t, tvals, 'VariableNames',{'Fund','Allocation','Dollar_Value'})
Tval =
    Fund    Allocation    Dollar_Value
    ____    __________    ____________
    BHP      0.22894      [1x11 char]
    NAB      0.19151      [1x11 char]
    CSR      0.38988      [1x11 char]
    AGL      0.13727      [1x11 char]
    NCP     0.052397      [1x11 char]
Tval.Dollar_Value
ans = $22894412.3
$19150910.7
$38988264.5
$13726664.3
$5239748.2
How to get the strings to display? Thanks!
  댓글 수: 2
KSSV
KSSV 2016년 10월 21일
Can you give the whole table information?
yudi vass
yudi vass 2016년 10월 21일
dol = repelem('$',5)';
tvals = num2str(f_vals, '%-.1f\n')
tvals2 = strcat(dol,tvals)
whos tvals2
Tval = table(name, x_t, tvals, 'VariableNames',{'Fund','Allocation','Dollar_Value'});
Tval =     Fund    Allocation    Dollar_Value
    ____    __________    ____________
    BHP      0.22894      22894412.3  
    NAB      0.19151      19150910.7  
    CSR      0.38988      38988264.5  
    AGL      0.13727      13726664.3  
    NCP     0.052397      5239748.2
  
The problem is when I try to add $ to the numbers.

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

채택된 답변

Peter Perkins
Peter Perkins 2016년 10월 21일
You're using a char array, almost certainly not what you want for a variety of reasons. Mostly, char matrices require awkward padding with spaces and don't have many of the text processing functions that cell arrays of char vectors have. (I won't even get into why you're using text for a numeric quantity, presumably you have good reasons.) If you use a cell array of char vectors, table will display the full text:
>> table([1;2;3],{'abcdefgh';'abcdefghabcdefgh';'abcdefghabcdefghabcdefgh'})
ans =
Var1 Var2
____ __________________________
1 'abcdefgh'
2 'abcdefghabcdefgh'
3 'abcdefghabcdefghabcdefgh'
In the most recent version of MATLAB, R2016b, you're probably even better off using the new string type. Hope this helps.
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 10월 25일
There is no way to display a number array with a $ in front.
tvals = cellstr( num2str(f_vals(:), '$%-.1f') );
Tval = table(name, x_t, tvals, 'VariableNames',{'Fund','Allocation','Dollar_Value'});
yudi vass
yudi vass 2016년 10월 27일
That's exactly what I was trying to do. cheers

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 21일
When displaying cells or struct or tables containing strings or arrays of numbers, MATLAB summarizes if it does not think that the display will easily fit.
My investigation a month or so ago found that the point at which it started summarizing depended on the width of the command output window, so if you make your window wider you might get more characters displayed.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by