how to add a table (figure) and display my text into 5 lines ?

2 visualizaciones (últimos 30 días)
Noa Prasquier
Noa Prasquier el 23 de Abr. de 2021
Respondida: Ishu el 27 de Feb. de 2024
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

Respuestas (1)

Ishu
Ishu el 27 de Feb. de 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:

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by