Crate a changeable label in programmable App (GUI)

8 ビュー (過去 30 日間)
Parker
Parker 2022 年 8 月 5 日
回答済み: Mohammad Sami 2022 年 8 月 6 日
Hi, I'm new to program with GUIs.
I'm trying to create a very easy GUI that can have an updateable outcome. For example I have two uieditfield functinos, so a user can type a value for X and a value for Y in each uieditfied.
And then there is a uilabel that gives a Z value of X+Y in strings.
This is my attempt so far:
fig = uifigure;
edt1...
edt2...
btnUpdate = uibutton(fig,'push',...
'Text','Update',...
'Position',[350, 170, 100, 22],...
'ButtonPushedFcn', @(btn,event) UpdateButtonPushed(label1));
function UpdateButtonPushed(label1)
Z = X + Y;
text = sprintf('%s\n',Z);
label1 = uilabel(Text',text,'Position',[100 100 100 32]);
end
I skipped the two uieditfield functions they are very standard.
The above code works but only problem is that the updated label will be created in a new pop up window.
I have tried this following code try to force the label being created within the GUI but it just pops up an error message
label1 = uilabel('Parent', fig, Text',text,'Position',[100 100 100 32]);
Unrecognized function or variable 'fig'.
Please let me know if there's a way around this or I if needed to use some function else than uilabel.
Thank you for your time in advence.

回答 (1 件)

Mohammad Sami
Mohammad Sami 2022 年 8 月 6 日
I am assuming that you are using app designer. If so you should place the uilabel in the UI on the canvas. Then you can access it in your code without having to create new uilabel every time someone updates the values.
if true
app.label1.Text = text;
end

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by