How to display string with ↵ by multiple lines in command window?
12 次查看(过去 30 天)
显示 更早的评论
Hi, I have a few transfer functions and some other numbers which I want to save to table and then display in command window. Transfer functions have 3 lines and It's apears by this ↵ characters. I need to remove them and show by 3 lines in one cell. For example:
tf_1 = tf(1.002, [0.578, 1], 'IODelay', 0.5);
tf_2 = tf(0.987, [0.578, 1], 'IODelay', 0.2);
tf_3 = tf(0.991, [0.612, 1], 'IODelay', 0.15);
tf_1_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_1);
tf_2_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_2);
tf_3_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_3);
a = [0.9991, 0.897, 0.957];
b = {'a', 'b', 'c'};
transfer_fun_T = table([string(tf_1_text); string(a(1)); b(1)],...
[string(tf_2_text); string(a(2)); b(2)],...
[string(tf_3_text); string(a(3)); b(3)],...
'VariableNames', {'Transfer fun 1', 'Transfer fun 2', 'Transfer fun 3'},...
'RowName', {'Transfer function', 'some numbers', 'some strings'});
disp(transfer_fun_T);
Output of example code:

So I need a cell with multiple lines to make it readable.
Thank you for your answer
0 个评论
采纳的回答
Walter Roberson
2021-1-2
The table() display code changes newlines to ↵ as does the string() display code for non-scalar strings
SP = @(S) char(reshape(strsplit(char(string(S)), '\n'), [], 1));
tf_1 = tf(1.002, [0.578, 1], 'IODelay', 0.5);
tf_2 = tf(0.987, [0.578, 1], 'IODelay', 0.2);
tf_3 = tf(0.991, [0.612, 1], 'IODelay', 0.15);
tf_1_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_1);
tf_2_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_2);
tf_3_text = matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(tf_3);
a = [0.9991, 0.897, 0.957];
b = {'a', 'b', 'c'};
transfer_fun_T = table({SP(tf_1_text); SP(a(1)); SP(b(1))},...
{SP(tf_2_text); SP(a(2)); SP(b(2))},...
{SP(tf_3_text); SP(a(3)); SP(b(3))},...
'VariableNames', {'Transfer fun 1', 'Transfer fun 2', 'Transfer fun 3'},...
'RowName', {'Transfer function', 'some numbers', 'some strings'});
disp(transfer_fun_T);
transfer_fun_T{'Transfer function', 'Transfer fun 1'}{1}
Now what? We have managed to store the lines as individual rows within a cell array, but table() is not going to display those lines for you. To get it to display the individual lines, you would need to create individual rows within the table() object, like 'Transfer function (line 1)' which in turn would require worrying padding shorter variables to consistent number of rows...
3 个评论
Walter Roberson
2021-1-3
table() objects are designed for computation, not for display purposes.
Report Generator gives much more flexibility on display of tables.
So there is no way to display table with transfer functions?
[P,Q] = tfdata(tf_1);
"(" + string(vpa(poly2sym(P{1}, S),5)) + ")/(" + string(vpa(poly2sym(Q{1}, S),5)) + ")"
and use hasdelay(tf_1) to determine whether to construct "exp(" + -total_delay(tf_1) + "*s)*" as a prefix.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!