How do I Remove Double Quotes from my Table?

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 commentaire

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
>>

Connectez-vous pour commenter.

 Réponse acceptée

dpb
dpb le 28 Août 2018
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 commentaires

Mike Raymond
Mike Raymond le 10 Sep 2018
Thank you so much!
dpb
dpb le 11 Sep 2018
It might not be a bad enhancement request to simply ask for a report generator similar to that of table format, however.
Hello. I have the same problem. I have a char which I use a Delimiter ' , ' to split it into a cell and then when I use cell2table I constantly get ' ' quotes which I cannot remove. How can I remove this from my table some way? Or how can I turn this string below into a matlab table ?
Thank you in advance
' PHIPS-SCT,20210621144520818,829,1860,1,1,1,1,1,2,1,2,1,1,2,2,1,2,1,2,1,2,1,2,2,2,2,1,1,2,1,2,1,2,1,1,2047,'
dpb
dpb le 24 Juin 2021
Show us the exact code -- we can't debug or decode what we don't have/can't see...
If it is simply the display of a cellstr() value in a table variable, then reread the above -- it's a fignewton of the display format MATLAB uses and not part of the actual data itself.
S = ' PHIPS-SCT,20210621144520818,829,1860,1,1,1,1,1,2,1,2,1,1,2,2,1,2,1,2,1,2,1,2,2,2,2,1,1,2,1,2,1,2,1,1,2047,'
S = ' PHIPS-SCT,20210621144520818,829,1860,1,1,1,1,1,2,1,2,1,1,2,2,1,2,1,2,1,2,1,2,2,2,2,1,1,2,1,2,1,2,1,1,2047,'
C = strsplit(S, ',')
C = 1×38 cell array
{' PHIPS-SCT'} {'20210621144520818'} {'829'} {'1860'} {'1'} {'1'} {'1'} {'1'} {'1'} {'2'} {'1'} {'2'} {'1'} {'1'} {'2'} {'2'} {'1'} {'2'} {'1'} {'2'} {'1'} {'2'} {'1'} {'2'} {'2'} {'2'} {'2'} {'1'} {'1'} {'2'} {'1'} {'2'} {'1'} {'2'} {'1'} {'1'} {'2047'} {0×0 char}
nC = length(C);
T = table('Size', [0 nC], 'VariableTypes', repmat("categorical", 1, nC));
T = [T; num2cell(categorical(C))]
T = 1×38 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 Var34 Var35 Var36 Var37 Var38 _________ _________________ ____ ____ ____ ____ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ ___________ PHIPS-SCT 20210621144520818 829 1860 1 1 1 1 1 2 1 2 1 1 2 2 1 2 1 2 1 2 1 2 2 2 2 1 1 2 1 2 1 2 1 1 2047 <undefined>
Elysi Cochin
Elysi Cochin le 12 Oct 2022
Modifié(e) : Elysi Cochin le 12 Oct 2022
@Walter Roberson, what change should I make to the above code to display 2 rows with row name and column names
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 = table( .....
What is the remaining code I need to write
what change should I make in the below lines
nC = length(C);
T = table('Size', [0 nC], 'VariableTypes', repmat("categorical", 1, nC));
T = [T; num2cell(categorical(C))]
dpb
dpb le 12 Oct 2022
Modifié(e) : dpb le 12 Oct 2022
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'};
Start by 'splaining what want to do with the ± notation above; it won't be of much use for anything except visualization as string data whether cell or string or even categorical; it would seem separating out as the value and error value for each variable would make more sense???
But,
T=cell2table([row1;row2],'VariableNames',varnames,'RowNames',rownames)
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'}
produces it in cellstr form presuming it is more or less rhetorical Q?
NOTA BENE however -- to do anything efficiently with the Row data; do NOT create multiple sequentially-number variables; use an array or table or almost any other storage mechanism instead; using the multiple names as above means a very difficult time in using the resulting data unless there are only one or two index variables like a start/stop subscript or somesuch. As a general convention, it is a practice to be avoided.
Elysi Cochin
Elysi Cochin le 13 Oct 2022
Modifié(e) : Elysi Cochin le 13 Oct 2022
I wanted to display the rows in a table with column names and row names, but I want to remove the single quotes and curly brackets, as displayed by @Walter Roberson with a single row data.
I have more rows and I also wanted to specify the row name and column name.
I have attached 2 datas to be displayed without the curly brackets and quotes in a table.
Please help me to solve it.
dpb
dpb le 13 Oct 2022
Modifié(e) : dpb le 13 Oct 2022
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 le 14 Oct 2022
Thank you so much Sir. Its just for display purpose. Thanks for the help.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by