How do I remove brackets/braces/quotes from table entries?
40 次查看(过去 30 天)
显示 更早的评论
I am using cell2table to create a table displaying the reslults computed by my script. However, the table always comes out with brackets, quotes, etc depending on the class of the variable in that cell. Below are a few rows of the output table followed by a sample of the code used to compute the variables. This is all in a big "for" loop, and coord, t_A, rul, and forecast are all preallocated as cell arrays.
Test Point, (X,Y) Classification Time Until Aged Remaining Useful Life
___________________ ______________ _______________ _____________________
{'1, (2,2)' } "Healthy" {'N/A' } {'N/A' }
{'2, (3,3)' } "Healthy" {[ 2.4354]} {[ 7.5699]}
{'3, (4,5)' } "Healthy" {[0.78741]} {[ 4.916]}
coord(i) = {[num2str(i) ', (' num2str(x) ',' num2str(y) ')']};
classname(i) = "Aged";
if i==1
t_A{1} = 'N/A';
rul{1} = 'N/A';
else
if class(i) == 1
t_A{i} = %numerical value
rul{i} = %numerical value
end
end
forecast(i,:) = {coord{i},classname(i),t_A{i},rul{i}};
results = cell2table(forecast(1:i,:),'VariableNames',{'Test Point, (X,Y)','Classification','Time Until Aged','Remaining Useful Life'});
采纳的回答
Walter Roberson
2020-10-25
Those marks are not really there. They are added by the display routines to make the output clearer.
table objects are not designed for presentation purposes. Either use Report Generator with custom formatting, or else write your own display routines.
3 个评论
Walter Roberson
2020-10-25
I did think of an alternative. You could evalc and then regexprep to delete those characters.
Peter Perkins
2020-11-20
Actually, Walter is (partially) incorrect. He's right that tabular display isn't really intended for presentation graphics, but the braces and brackets ought not to be there.
Keenan, your table is messed up. You have cell arrays where you should have numeric. The problem is that somewhere along the line you ended up with a cell array that was like
C =
4×4 cell array
{'Test Point, (X,Y)'} {'Classification'} {'Time Until Aged'} {'Remaining Useful Life'}
{'1, (2,2)' } {'Healthy' } {'N/A' } {'N/A' }
{'2, (3,3)' } {'Healthy' } {[ 2.4354]} {[ 7.5699]}
{'3, (4,5)' } {'Healthy' } {[ 0.78741]} {[ 4.916]}
And then
>> t = cell2table(C(2:end,:),'VariableNames',C(1,:))
t =
3×4 table
Test Point, (X,Y) Classification Time Until Aged Remaining Useful Life
_________________ ______________ _______________ _____________________
{'1, (2,2)'} {'Healthy'} {'N/A' } {'N/A' }
{'2, (3,3)'} {'Healthy'} {[ 2.4354]} {[7.5699]}
{'3, (4,5)'} {'Healthy'} {[0.78741]} {[ 4.916]}
because 'N/A' is text and so those entire two columns in C stay as cell vectors in T because text and numeric can't be combined. What you want is
>> C{2,3} = NaN; C{2,4} = NaN;
>> t = cell2table(C(2:end,:),'VariableNames',C(1,:));
>> t.Classification = categorical(t.Classification);
>> t.(1) = string(t.(1))
t =
3×4 table
Test Point, (X,Y) Classification Time Until Aged Remaining Useful Life
_________________ ______________ _______________ _____________________
"1, (2,2)" Healthy NaN NaN
"2, (3,3)" Healthy 2.4354 7.5699
"3, (4,5)" Healthy 0.78741 4.916
I used categorical on one of the variables, which displays the category names without quotes. You could do the same with the test point var, or not.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!