creating a GUI with edit box and push button
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I need to write a code that will create a editable text box and I have a few questions about it
How and where to insert a function that will place my text into 5 lines?
Here's my code and the output is an error :
I think there are a lot of things to modify so I would really appreciate your help :
clc;clear all; close all;
figures=figure()
global text
set(gcf,'CurrentAxes',figures.Children)
%button.Callback=@testoccurences2;
button = uicontrol('Style','pushbutton','String','Count Letters','Position',[50 50 300 75]);
editbox=uicontrol('Style','edit','String','0','Position',[200 200 100 100]);
editbox.Callback=@ (src,evt)entertext
button.Callback=@(src,evt) get_occurences(text);
function[]=entertext()
global text
prompt={'Enter poem'};
text=inputdlg(prompt);
end
function[]=get_occurences(String)
vect_A=[];
i=1;
for letter=String(1:length(String))
letter=lower(letter);
for alphabet=['a':'z']
A=count(String,alphabet);
vect_A(i)=A; %stock the value
i=i+1;
end
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'}
end
When the user presses the button, a table needs to show up, with the alphabet and the nu;ber of occurences of each letter in the text
0 Kommentare
Antworten (1)
Jan
am 21 Apr. 2021
Bearbeitet: Jan
am 21 Apr. 2021
Omit the brute clearing header
clc;clear all; close all;
It is a waste of time, kills other windows, and has not serious benefit. It is recommended by teachers, which remember to have learned this as pupils also. This is called "Cargo Cult programming".
Avoid global variables, because they are a shot in your knee.
Using "text" as a variable does not let you use the command text() anymore. This is called "shadowing" and a frequent source of unexpected behavior.
String(1:length(String)) is the same as String.
'a':'z' is a vector already. There is no need to included it in [ and ] , because this is Matlab concatenation operator. But you do not concatenate 'a':'z' to anything.
Now the actual problem:
for letter=String(1:length(String)) % < This is not used inside the inner loop
letter=lower(letter); % < So you can omit the outer loop
for alphabet=['a':'z']
A=count(String,alphabet);
vect_A(i)=A; %stock the value
i=i+1;
end
end
Althout the for letter loop is not used at all, the counting is performed as often as the String has characters. So vect_A contains the list of numers of repetitions repeatedly. vect_A has 26 * numel(String) values finally. Therefore assiging the 26 characters as names of the variables of the table cannot work.
Solution: Omit the outer loop.
function get_occurences(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'}
end
Or a simplified version:
function get_occurences(String)
CharList = 'a':'z';
vect_A = zeros(1, numel(CharList)); % pre-allocate
for k = 1:numel(CharList)
vect_A(k) = count(String, CharList(k));
end
occ_table = array2table(vect_A);
occ_table.Properties.VariableNames = num2cell(CharList);
end
2 Kommentare
Jan
am 22 Apr. 2021
What is the purpose of
set(gcf,'CurrentAxes',figures.Children)
Directly after opening a figure, it does not have Children. You set the CurrentAxes to the empoty matrix, which is the default already.
What is "text" in this line:
button.Callback=@(src,evt) get_occurences1(text);
? Please try somethiung like:
button.Callback=@(src,evt) get_occurences1(editbox.String);
I cannot guess, where "enter a poem" comes from. But you can find this out easily: Set a breakpoint in the first line of the code and step through it line by line. Then you will see, which line produces this output.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!