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!