App designer: randomly generated sentence GUI

Hello everyone,
I have created a random phrase generator in Matlab; it starts by choosing between two types of sentences (using randi) and then uses randi again to construct one of the two types and displays it in the command window.
I am having, however, hell of a time trying to create a GUI that displays the generated text somewhere. I added a callback to my random phrase generator function and associated it with a button and a textbox; the function runs properly when I press the button but nothing appears in the textbox.
Anyone knows?
I have been looking it up but it seems that the app designer has been changing every year.

2 个评论

Can you provide a copy your callback function to see if anything is wrong with it.
Yes, here it is below. Thing is i am not sure exactly where to copy and paste it and what I should modify etc, what to tie to what... I guess the button and the text box have to share the same callback etc.
% Random phrase generator
clear all;
clc;
% Choose between the two types
rng('shuffle')
Ch=randi(35);
if Ch==10
rng('shuffle')
Type_1 = {'phrase 1', 'phrase 2',.... 'phrase n'};
Nugget = [Type_1{randi(numel(Type_1))}];
fprintf(Nugget)
elseif Ch~=10
% Create 3 types of cells to put them in a cell array
Start = {'phrase 1 ','phrase 2 ','phrase 3 '...'phrase n'};
Middle = {'phrase 1 ','phrase 2 ','phrase 3 '...'phrase n'};
Final={'phrase 1 ','phrase 2','phrase 3 '...'phrase n '};
% Put them all in a cell array
A={Start,Middle,Final};
% Create variable with a combination of the three parts called 'Sentence'
rng('shuffle');
a=Start{randi(numel(Start))};
rng('shuffle');
b=Middle{randi(numel(Middle))};
rng('shuffle');
c=[Final{randi(numel(Final))} '.'];
Sentence = [a b c];
% Print this variable
fprintf(Sentence)
end

请先登录,再进行评论。

 采纳的回答

If you are using app designer, to build your GUI you can do as follows.
Drag and drop a button and an "Edit Field (Text)" into your app.
If this is a blank app, the names would be Button and EditField.
Right click the button, goto callbacks and select Add ButtonPushedFcn callback.
The designer will switch to code view.
You can then simply put in your random generation code in the callback.
At the end just assign the Sentence output to the EditField as follows
function ButtonPushed(app, event)
% Your random sentence generator code here
% Sentence = ....
app.EditField.Value = Sentence;
end
This should update the edit field with a new sentence everytime you click the button.

3 个评论

Hello Mohammad,
Appreciate your response a lot, but I am afraid this, as it is, will still not work. Not sure if I am implementing this correctly though, so just to be clear:
  1. I added a callback to the button only-not the edit text box.
  2. I copied and pasted the entire code there, so maybe something is lagging with the way the code is written.
I tried messing about a bit with it, changing fprintf in my code to sprintf (although this does not seem to make a difference), putting the statement
app.EditField.Value = Sentence;
before or after the 'end' but this did not make a difference either.
I am really curious about those inputs in the function ButtonPushed(app, event) -app and event-only if they have something to do with my problem I guess for now. What do they do and do I need to explicitly assign something there?
In the end I also tried adding a callback to the edit field as well (both in ValueChangedFcn and in ValueChangingFcn, (first one, then both)- that did not work either.
What is the name of your edit text box? Is it EditField? It's case sensitive so check that also.
So thanks Mohammad, this finally worked. However, as Image Analyst pointed out below, the 'clear all' command had to go.

请先登录,再进行评论。

更多回答(1 个)

Assuming your edit text box is named edtText, try this. First create your sentence however you do it, then:
app.edtText.String = yourSentence; % Set string property, not value property.
Also, your callback for the push button where you create this sentence and send it to the GUI should not have a "clear all" command in it.

4 个评论

Thanks a lot for this...
So the String did not work (I got the following message):
Unrecognized property 'string' for class 'matlab.ui.control.EditField'.
Error in app1/ButtonPushed (line 66)
app.EditField.string = Sentence;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
However, your advice to delete 'clear all' was bloody excellent. I did that, and then used Mohammad's ''app.EditField.Value = Sentence;'' tip and it worked beautifully. I had to accept this as an answer but you also saved me a lot of grief.
I am guessing 'clear all' clears the box, but it is unclear to me precisely why it does this -of course initially that command cleared the workspace for me and I get that it functions differently in the app designer but I am wondering why it does this?
You should know that MATLAB is case sensitive. So while I wrote
app.edtText.String = yourSentence; % S in String is UPPER CASE.
you wrote
app.edtText.string = yourSentence; % String all lower case - BAD!
I believe that is the cause of the error.
Hello!
I did try both- capital s as well as lower case s, and got the same message (I guess I erroneously copied and pasted the latter one). Apologies!
Good to hear that it works.
I did not notice the clear all.
The reason why it broke your code, is that it will clear the variables passed into your function (app and event). So you will no longer be able to access these variables in the later part of your function. In this case we needed the "app" variable to access the edit text field.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by