How do I Remove Double Quotes from my Table?

조회 수: 57 (최근 30일)
Mike Raymond
Mike Raymond 2018년 8월 28일
댓글: Csaba Noszaly 2024년 10월 4일
I started with this seemingly simple problem as a way to learn some new skills in MatLab:
Given a list of names in a cell array, sort the list by the last name. My end-goal was to have a table with two columns, last and first name, sorted by last name. I think I'm close to a solution, but the table output includes double quotation marks. I'd like to understand a simple way of removing the quotation marks.
list = {'Barney Google','Snuffy Smith','Dagwood Bumstead'};
list = list';
list2 = split(list);
T = array2table(list2,...
'VariableNames',{'FirstName','LastName'});
T = sortrows(T,'LastName');
  댓글 수: 1
Csaba Noszaly
Csaba Noszaly 2024년 10월 4일
starting at 2021a, Matlab has formattedDisplayText, whose output can be adjusted as needed:
>> mtx=["1" "2"; "alma" "körte"];
>> t=formattedDisplayText(array2table(mtx))
t =
" mtx1 mtx2
______ _______
"1" "2"
"alma" "körte"
"
>> disp(replace(t, '"', " "))
mtx1 mtx2
______ _______
1 2
alma körte
>>

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

채택된 답변

dpb
dpb 2018년 8월 28일
The quotes are a fignewton of the Matlab display formatting; you can't eliminate them from the command window excepting by either writing the string with a formatting expression or via disp or the like...but while the type indicator characters are displayed, they are not part of the data itself. Illustration with a slight modification; use the new(ish) string class instead of cellstr array...
slist = ["Barney Google";"Snuffy Smith";"Dagwood Bumstead"];
T = array2table(split(slist),'VariableNames',{'First','Last'});
>> T.First(1) % observe the " to indicate a string
ans =
"Barney"
>> disp(T.First(1)) % with disp() only the data shown
Barney
>>
>> strfind(T.First(1),'"') % show there's no " stored...
ans =
[]
>>
>> fprintf('%s\n',T.Last(1)) % or write the data; no quotes, either.
Google
>>
The table output format is not intended to be a printed report; it's intended as a working interface to the use of the table and the reminders of the datatype are consistent with the "ordinary" display of similarly-typed variables in the command window.
  댓글 수: 10
dpb
dpb 2022년 10월 13일
편집: dpb 2022년 10월 13일
It'll be useless for anything else, much, but what Walter did above was to turn everything to a categorical variable...
row1 = {'0.9984±0.0164' '0.9802±0.0198' '0.9762±0.0238' '0.9962±0.0238'};
row2 = {'0.8746±0.0198' '0.8538±0.0289' '0.8596±0.0886' '0.8596±0.0086'};
varnames = {'Col1' 'Col2' 'Col3' 'Col4'};
rownames = {'Row1' 'Row2'};
T=cell2table([row1;row2],'VariableNames',varnames,'RowNames',rownames);
T=convertvars(T,T.Properties.VariableNames,'categorical')
T = 2×4 table
Col1 Col2 Col3 Col4 _____________ _____________ _____________ _____________ Row1 0.9984±0.0164 0.9802±0.0198 0.9762±0.0238 0.9962±0.0238 Row2 0.8746±0.0198 0.8538±0.0289 0.8596±0.0886 0.8596±0.0086
But, as the Answer says, the table is NOT a presentation object and there's going to be nothing you can do useful with the result after having done other than look at it in the command window/on the screen.
Elysi Cochin
Elysi Cochin 2022년 10월 14일
Thank you so much Sir. Its just for display purpose. Thanks for the help.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 8월 28일

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by