how to add a table (figure) and display my text into 5 lines ?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I am wondering how to create a table with uitable and fill it when the user presses the button.
before he presses the button, I want to display a table with only 4 numbered rows and 2 colums with the names : 'Letter', 'Number of occurences'
And then I want to fill it with the output of this code : (occ_table)
I really appreciate your help
figures=figure();
%set(gcf,'CurrentAxes',figures.Children)
%button.Callback=@get_occurences;
button = uicontrol('Style','pushbutton','String','Count Letters','Position',[50 100 300 75]);
editbox=uicontrol('Style','edit','String','Enter your poem','Position',[50 200 300 200]);
%editbox.Callback=@ (src,evt)entertext
button.Callback=@(src,evt) get_occurences1(editbox.String);
%table_fig=uitable()
function[]=entertext()
prompt={'Enter poem'};
txt=inputdlg(prompt);
end
function get_occurences1(String)
vect_A = [];
i = 1;
for alphabet=['a':'z']
A = count(String,alphabet);
vect_A(i)=A; %stock the value
i=i+1;
end
occ_table = array2table(vect_A);
occ_table.Properties.VariableNames = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
%uitable('Data',occ_table{:,:},'ColumnName',{'Letter','Number of occurences'},'RowNames',occ_table.Properties.VariableNames,'Units','Normalized')
end
0 Commenti
Risposte (1)
Ishu
il 27 Feb 2024
Hey,
I understand that you want to create a table and fill it with the output of the above code. For that first you need to create a table in the figure and fill it with empty cell array. Then in the callback function you can update the data in the table.
function myApp
fig = figure();
button = uicontrol('Style', 'pushbutton', 'String', 'Count Letters', 'Position', [50 240 300 50]);
editbox = uicontrol('Style', 'edit', 'String', 'Enter your poem', 'Position', [50 300 300 100]);
% Create a table with predefined columns
columnNames = {'Letter', 'Number of Occurrences'};
initialData = cell(4, 2); % Empty cell array for initial table data
table_fig = uitable('Data', initialData, 'ColumnName', columnNames, 'Position', [50 70 300 150]);
button.Callback = @(src, evt) get_occurrences1(editbox, table_fig);
end
function get_occurrences1(editbox, table_fig)
String = editbox.String;
vect_A = 1:26;
i = 1;
for alphabet = 'a':'z'
A = count(String, alphabet);
vect_A(i) = A;
i = i + 1;
end
% Create a table with the counts
occ_table = num2cell(vect_A');
letters = cellstr(('a':'z')');
occ_table = [letters, occ_table];
table_fig.Data = occ_table; % Update the table data
end
For more information you can refer these MathWorks documentation:
0 Commenti
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!