Create table columns with some entries blank (no quote symbols)

조회 수: 4(최근 30일)
FM
FM 2022년 3월 29일
편집: FM 2022년 3월 29일
I have a table with some Boolean variables. They show up as `true` or `false`. I would like the trues to show up as (say) "Y" and the falses to show up as blanks. It's easier to see patterns.
I tried replacing the Boolean columns with columns of type `string`, but each string is adorned with quotes, even the blank ones. Too much visual noise. Same if I use a cell array of char arrays.
I often have good luck by applying `categorical` to string columns to get rid of quotes, but that doesn't work when there are empty strings or single spaces (" ").
Is there any way to have table columns with text labels, but some blanks without quotes?
I am using Matlab 2019a. Here it is, using string column arrays. I get similar results with cell column arrays of char arrays.
>> Tab = table;
>> Tab.YesNo1 = [false;true];
>> Tab.YesNo2 = strings( height( Tab ), 1 )
>> Tab.YesNo2( Tab.YesNo1 ) = "Y"
YesNo1 YesNo2
______ ______
false ""
true "Y"
>> Tab.YesNo2 = categorical( Tab.YesNo2 )
YesNo1 YesNo2
______ ___________
false <undefined>
true Y
>> Tab.YesNo2 = repmat(" ", height( Tab ), 1 );
>> Tab.YesNo2( Tab.YesNo1 ) = "Y"
YesNo1 YesNo2
______ ______
false " "
true "Y"
>> Tab.YesNo2 = categorical( Tab.YesNo2 )
YesNo1 YesNo2
______ ___________
false <undefined>
true Y
  댓글 수: 1
AndresVar
AndresVar 2022년 3월 29일
Can you give example? Is this what you mean?
clear;
a = [true; false];
t = table(a) % table with logicals
t = 2×1 table
a _____ true false
b = repmat("",size(a)); % test with string
c = repmat(' ',size(a)); % test with char
idxTrue = t.a==true; % get position of true values
b(idxTrue) = "Y";
c(idxTrue) = 'Y';
% add the new columns
t.b = b;
t.c = c;
t
t = 2×3 table
a b c _____ ___ _ true "Y" Y false ""
It still may not be effective to look visualize the table in the printed format, maybe you should plot something?

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

답변(1개)

Arif Hoq
Arif Hoq 2022년 3월 29일
try this:
str={'Y'};
col=[{'True'};{'True'};{'False'};{'True'};{'False'};{'True'}];
T=table(col);
for i=1:size(col,1)
if string(table2array(T(i,1))) ==string({'True'})
T(i,1)={'Y'};
else
T(i,1)={''};
end
end
  댓글 수: 3
FM
FM 2022년 3월 29일
편집: FM 2022년 3월 29일
I added my sample code and output to the posted question.
I get what you say about plotting, but I'm trying to accomplish something simple without taking the level of effort to the next level. Otherwise, it's not worth it and I have to live with the visual noise.
For example, I could copy and paste it into Vim and use it's editorial powers.
I could copy and paste it into LaTeX and use Vim's editorial powers.
I could go through COM and dump the table into a spreadsheet, where the unicode characters might render.
I'm hoping that there is a solution for proper formatting in the command window.
AFTERNOTE: I'm finding that if the entire column is a single char array, the quotes don't show. But monolithic char arrays are not easy to deal with. You have to take into account space padding in equality testing, and the code to change a single row is uglier. Hoping there is a better solution.

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

범주

Find more on Characters and Strings in Help Center and File Exchange

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by