App Designer component generated by code not displaying

As a simple example, the expected label from the code below does not display. This is my first attempt to generate a component by code. Also first question to the forum. What am I missing?
methods (Access = private)
function startupFcn(app)
% Display a label as a simple example
lbl = uilabel(app.UIFigure);
lbl.Text = 'Hello, this is a label!';
lbl.Position = [100 300 200 30]; % [x y width height]
lbl.FontSize = 14;
lbl.FontColor = [0 0 1]; % Blue text
lbl = uilabel(app.Panel);
lbl.Text = 'Label inside a panel';
lbl.Position = [20 50 150 22];
end
end

 Risposta accettata

Matt J
Matt J il 9 Dic 2025
Modificato: Matt J il 9 Dic 2025
It is conspicuous to me that you are complaining about missing labels, but not about the missing uipanel!. Did you pre-populate the canvas with one? Assuming you did (and that it is named 'Panel') then there's nothing wrong with the code you have shown. Here is what your code gives me.

8 Commenti

I think your point leads to myself not knowing some fundamental step. If I run the first code in Matlab, I see a label appear. If I run the second code in App Designer, I do not see a label. I must be missing something very basic. My first attempt at creating a component using self written code in App Designer. Dragging components over and letting App Designer write most of the supporting code works fine.
% This create a label if run in Matlab
lbl = uilabel;
lbl.Text = 'Hello, this is a label!';
lbl.Position = [100 300 200 30];
lbl.FontSize = 14;
lbl.FontColor = [0 0 1];
% This does not create a label when added to the default code provided
% when you create a new blank app within App Designer
methods (Access = private)
function startupFcn(app)
lbl = uilabel(app.UIFigure); % Note 'app.UIFigure' in this case
lbl.Text = 'Hello, this is a label!';
lbl.Position = [100 300 200 30];
lbl.FontSize = 14;
lbl.FontColor = [0 0 1];
end
end
Matt J
Matt J il 9 Dic 2025
Modificato: Matt J il 9 Dic 2025
What steps did you follow to add the startupFcn to the App Designer code view? Can you share a screenshot of your entire code view screen, or at least a substantial part containing the startupFcn? Mine looks like this:
Sorry for not intially sharing more of the code. My steps were open a new app, choose a blank app, choose to add a function, and within the methods declarations then add the startupFcn .
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
end
methods (Access = private)
% I ADDED FROM HERE...
% This does not create a label when added to the default code provided
% when you create a new blank app within App Designer
function startupFcn(app)
lbl = uilabel(app.UIFigure); % Note 'app.UIFigure' in this case
lbl.Text = 'Hello, this is a label!';
lbl.Position = [100 300 200 30];
lbl.FontSize = 14;
lbl.FontColor = [0 0 1];
end
% TO HERE....
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
No, the startupFcn is a callback of the app. It cannot be added as an ordinary method. To insert it, right click on the top-level app handle in the Component Browser and select the dropdown options shown:
I just now added a panel by sliding one over from the component library, App Designer automatically added the corresponding code, then attemped to display the label in the panel. Still no label. Can it be the code?, or some setting or option not initiated?
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
end
methods (Access = private)
% This does not create a label when added to the default code provided
% when you create a new blank app within App Designer
function startupFcn(app)
lbl = uilabel(app.UIFigure); % Note 'app.UIFigure' in this case
lbl.Text = 'Hello, this is a label!';
lbl.Position = [100 300 200 30];
lbl.FontSize = 14;
lbl.FontColor = [0 0 1];
%%%%% App Designer inserted the Panel under properties, then i added this
% No luck
lbl=unlabel(app.Panel);
lbl.Text = 'Label in panel';
lbl.Position = [20 50 150 22];
end
end
Have you added the startupFcn as a callback, as outlined in my last comment?
Perfect! I very much appreciate your insights. Never would I have figured that out!
Bob
You're welcome, but please Accept-click the answer to indicate that it resolved your question.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2025b

Tag

Richiesto:

Bob
il 9 Dic 2025

Commentato:

il 10 Dic 2025

Community Treasure Hunt

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

Start Hunting!

Translated by