フィルターのクリア

How do I remove brackets/braces/quotes from table entries?

40 ビュー (過去 30 日間)
Keenan Barazi
Keenan Barazi 2020 年 10 月 25 日
コメント済み: Peter Perkins 2020 年 11 月 20 日
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
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
Walter Roberson 2020 年 10 月 25 日
I did think of an alternative. You could evalc and then regexprep to delete those characters.
Peter Perkins
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 ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by