How to display string with ↵ by multiple lines in command window?
8 views (last 30 days)
Show older comments
Jan Brieznik
on 2 Jan 2021
Commented: Jan Brieznik
on 3 Jan 2021
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 Comments
Accepted Answer
Walter Roberson
on 2 Jan 2021
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 Comments
Walter Roberson
on 3 Jan 2021
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.
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!