App designer: randomly generated sentence GUI
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Mohammad Sami
2019년 12월 26일
Can you provide a copy your callback function to see if anything is wrong with it.
채택된 답변
Mohammad Sami
2019년 12월 26일
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
Image Analyst
2019년 12월 26일
What is the name of your edit text box? Is it EditField? It's case sensitive so check that also.
추가 답변 (1개)
Image Analyst
2019년 12월 26일
편집: Image Analyst
2019년 12월 26일
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
Mohammad Sami
2019년 12월 30일
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!