How to write a table in a textarea on a Matlab App?

Hello all,
I am creating a Matlab App to perform some calculations, the calculation results are included in a table, and it includes numbers and text. I need to write the content of that table in a textarea so that the user can see the results after the calculation finishes. I am currently using the "compose" function, but I get a message error saying that "conversion to double from table is not possible".
Below are the three rows in the code:
formatSpec='%.0f;%s;%.0f;%s;%.2f;%.2f;%.2f;%.f2;%.2f';
Out_Zsc = table(Out_Zsc_Dou(:,1,cas),Out_Zsc_Str(:,1,cas),Out_Zsc_Dou(:,2,cas),Out_Zsc_Str(:,2,cas),Out_Zsc_Dou(:,3,cas),Out_Zsc_Dou(:,4,cas),Out_Zsc_Dou(:,5,cas),Out_Zsc_Dou(:,6,cas),Out_Zsc_Dou(:,7,cas),'VariableNames',{'Coil','Winding','Active Turn','Conductor', 'Power (MVA)', 'Voltage (kV)','Current (A)', 'Amp-Turn (A)', 'Acc. Amp-Turn (A/mm)'})
app.Zsc.Value=compose(formatSpec,Out_Zsc);
I am struggling to find the solution, I wonder if maybe the issues is the variables names which is included above the table. If I print the table in the command window, it works well:
Coil Winding Active Turn Conductor Power (MVA) Voltage (kV) Current (A) Amp-Turn (A) Acc. Amp-Turn (A/mm)
__________ _______ ___________ _________ ___________ ____________ ___________ ____________ ____________________
1.0000e+00 "LV" 5.0000e+00 "LV" 2.3750e+02 2.5000e+01 9.5000e+03 4.7500e+04 -6.4189e+01
2.0000e+00 "LV" 5.0000e+00 "LV" 2.3750e+02 2.5000e+01 9.5000e+03 4.7500e+04 -1.2838e+02
3.0000e+00 "HV" 2.6000e+01 "HN" 2.3750e+02 2.2047e+02 1.0772e+03 2.8008e+04 -9.0529e+01
4.0000e+00 "HV" 2.5000e+01 "HN" 2.3750e+02 2.2047e+02 1.0772e+03 2.6931e+04 -5.4136e+01
5.0000e+00 "HV" 2.4000e+01 "HN" 2.3750e+02 2.2047e+02 1.0772e+03 2.5854e+04 -1.9198e+01
6.0000e+00 "HV" 2.3000e+01 "HN" 2.3750e+02 2.2047e+02 1.0772e+03 2.4777e+04 1.4284e+01
7.0000e+00 "HV" 2.1000e+01 "HL" 2.3750e+02 2.2047e+02 1.0772e+03 2.2622e+04 4.4855e+01
8.0000e+00 "HV" 2.0000e+01 "HL" 2.3750e+02 2.2047e+02 1.0772e+03 2.1545e+04 7.3969e+01
9.0000e+00 "HV" 1.9000e+01 "HL" 2.3750e+02 2.2047e+02 1.0772e+03 2.0468e+04 1.0163e+02
1.0000e+01 "HV" 1.8000e+01 "HL" 2.3750e+02 2.2047e+02 1.0772e+03 1.9390e+04 1.2783e+02
1.1000e+01 "LV" 5.0000e+00 "LV" 2.3750e+02 2.5000e+01 9.5000e+03 4.7500e+04 6.3643e+01
1.2000e+01 "LV" 5.0000e+00 "LV" 2.3750e+02 2.5000e+01 9.5000e+03 4.7500e+04 -5.4652e-01
Any idea what I am doing wrong? any other method to write this table in the textarea? thanks.

 Risposta accettata

An easy way to display a table in a text area is to use formattedDisplayText.
Example:
% a table with numbers and text:
T = array2table(rand(10));
T.Var2 = string(T.Var2);
% a uifigure with text area:
f = uifigure();
t = uitextarea(f,'FontName','Courier');
% get the "string version" of the table:
str = formattedDisplayText(T);
str = regexprep(str,'</?strong>','');
% set it to the text area:
t.Value = str;
So your code might look like:
str = formattedDisplayText(Out_Zsc);
str = regexprep(str,'</?strong>','');
app.Zsc.Value = str;
assuming app.Zsc is your text area.

5 Commenti

Thanks Voss! It works! Much appreciated.
However there are two other issues now :)
  1. The table is now written in the textarea, but the columns are no longer maintained; do you know how if there is a way to keep centered columns?
  2. With the compose function, by using the formatspec, I wanted to truncate the decimals, but now it seems that is not available with your suggested method.
In any case, it is way better than before, thanks!
Pablo
How about using a uitable instead of a text area?
That works better, thanks! Initially I wanted to create a single text document with several tables listed, but I guess I will create as many uitables as needed, each one on a different panel. Thanks! your answer was the best, much appreciated to you all anyway.
Pablo
FYI, instead of manually removing the <strong> tags consider specifying the SuppressMarkup argument with a value of true.
t = array2table(magic(3));
text1 = formattedDisplayText(t)
text1 =
" Var1 Var2 Var3 ____ ____ ____ 8 1 6 3 5 7 4 9 2 "
text2 = formattedDisplayText(t, SuppressMarkup=true)
text2 =
" Var1 Var2 Var3 ____ ____ ____ 8 1 6 3 5 7 4 9 2 "
The two captured texts are not the same.
isequal(text1, text2)
ans = logical
0
The first has the strong tag, the second does not.
contains(text1, "<strong")
ans = logical
1
contains(text2, "<strong")
ans = logical
0
@Steven Lord: Very nice, thanks!

Accedi per commentare.

Più risposte (1)

app.Zsc.Value(cas)=compose(formatSpec,Out_Zsc(cas));
First off, your code Out_Zsc(cas) is an attempt to subscript at table with a single index. Subscripting a table with a single index is not permitted. If cas is row indices, then you would need Out_Zsc(cas,:). It seems unlikely, however, that cas is row indices, as you are using cas to select pages out of multidimensional data.
Next... your format specification to compose() is a character vector. The output of compose() with a character vector format, is a cell array of character vectors. You are assigning that cell array of character vectors to app.Zsc.Value(cas) which is probably not a cell array, and is probably the wrong size.
But your basic problem is that compose() cannot work with table() parameters.
You have two choices:
  • either convert the table to string array by using Out_Zsc{:,:} -- OR
  • pass compose the individual table columns
compose(formatSpec, Out_Zsc.Coil, Out_Zsc.Winding, Out_Zsc.('Active Turn'), Out_Zsc.Conductor, Out_Zsc.('Power (MVA)'), Out_Zsc.('Voltage (kV)'), Out_Zsc.('Current (A)'), Out_Zsc.('Amp-Turn (A)'), Out_Zsc.('Acc. Amp-Turn (A/mm)'))

1 Commento

Thanks Walter,
You are correct, there was a typo in the Out_Zsc, it should not have the "(cas)" portion, I edited my question.
Your second approach works, but the columns are displayed one after the next with a ";" separation only, and without the variable names, which is not ideal. I think i will use your first suggestion, converting the table to string array.
Thanks!

Accedi per commentare.

Prodotti

Release

R2024b

Richiesto:

il 5 Feb 2025

Commentato:

il 5 Feb 2025

Community Treasure Hunt

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

Start Hunting!

Translated by