MATLAB Answers

Problem accessing member in OOP

1 view (last 30 days)
Andreas Bernatzky
Andreas Bernatzky on 17 May 2021
Answered: Mohammad Sami on 18 May 2021
I am trying to write a custom login dialog UI as a class.
I have done all logins callbacks so far (or I think so) but if I press the login button the loginCallback gets executed.
My Problem is that I can not access any member inside of loginCallback ? Everything seems to be empty and I do not understand why.
Also tried it here with a test variable which is of an easy double type m_test is also empty but gets initialised correct by the constructor.
classdef LoginDialog
%LOGINDIALOG Summary of this class goes here
% This class provides the login UI and backend for the
% loginDialog
properties
main_fig;%mainfigure
m_login_btn;%login btn;
m_login_dropDown;%displays previous logins
m_login_chk;%gives ability to choose from previous logins
m_serverIp_txt;%enter login ip
m_serverUsr_txt;
m_serverpw_txt;
m_serverIp_lbl;%enter login ip
m_serverUsr_lbl;
m_serverpw_lbl;
m_logins; % keeps all the past login fields
m_test;
end
methods
%default constructor
function obj = LoginDialog()
%% definde UI
obj.main_fig = uifigure;
obj.main_fig.Resize = 0;
%% define button
obj.m_login_btn = uibutton(obj.main_fig);
obj.m_login_btn.Text = "Login";
obj.m_login_btn.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2,...
20,100,20];
obj.m_login_btn.ButtonPushedFcn = @obj.loginCallback;
%% define dropDown
obj.m_login_dropDown = uidropdown(obj.main_fig);
obj.m_login_dropDown.Position = [obj.main_fig.Position(3)-obj.m_login_dropDown.Position(3),...
obj.main_fig.Position(4)-obj.m_login_dropDown.Position(4),100,20];
obj.m_login_dropDown.Items = cell(1,0);
obj.m_login_dropDown.Visible = 0;
%% define server ip
obj.m_serverIp_lbl = uilabel(obj.main_fig);
obj.m_serverIp_lbl.Text = "Server Ip:";
obj.m_serverIp_lbl.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2 - 60,...
150,100,20];
%edit field
obj.m_serverIp_txt = uieditfield(obj.main_fig);
obj.m_serverIp_txt.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2,...
150,100,20];
%%define server username
obj.m_serverUsr_lbl = uilabel(obj.main_fig);
obj.m_serverUsr_lbl.Text = "Username:";
obj.m_serverUsr_lbl.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2 - 60,...
125,100,20];
% edit field
obj.m_serverUsr_txt = uieditfield(obj.main_fig);
obj.m_serverUsr_txt.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2,...
125,100,20];
%% define server pw
obj.m_serverpw_lbl = uilabel(obj.main_fig);
obj.m_serverpw_lbl.Text = "Password:";
obj.m_serverpw_lbl.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2 - 60,...
100,100,20];
% edit field
obj.m_serverpw_txt = uieditfield(obj.main_fig);
obj.m_serverpw_txt.Position = [obj.main_fig.Position(3)/2 - obj.m_login_btn.Position(3) / 2,...
100,100,20];
%% define chk box
obj.m_login_chk = uicheckbox(obj.main_fig);
obj.m_login_chk.Text = "Use existing login";
obj.m_login_chk.Position = [obj.m_login_dropDown.Position(1) - obj.m_login_chk.Position(3)*1.5,obj.m_login_dropDown.Position(2)-5,120,30];
obj.m_login_chk.ValueChangedFcn = @(cbx,event) cBoxChanged(obj,obj.m_login_chk);
% chk box value
%% load logins
if(exist('savedLogins.mat','file') ~= 0)
obj.m_logins = load('savedLogins');
if(~isempty(obj.m_logins.m_savedLogins))
identifiers = obj.m_logins.m_savedLogins{:,1};
obj.m_login_dropDown.Items = identifiers;
end
end
obj.m_test = 33;
end
%% method which gets executed on checkbox changed
function obj = cBoxChanged(obj,cbx)
val = cbx.Value;
if (val == 1)
obj.m_login_dropDown.Visible = 'on';
obj.m_serverIp_txt.Enable = 0;
obj.m_serverUsr_txt.Enable = 0;
obj.m_serverpw_txt.Enable = 0;
else
obj.m_login_dropDown.Visible = 'off';
obj.m_serverIp_txt.Enable = 1;
obj.m_serverUsr_txt.Enable = 1;
obj.m_serverpw_txt.Enable = 1;
end
end
function lastLogins = getLastLogin(obj)
if(exist('savedLogins.mat','file') ~= 0)
obj.m_logins = load('savedLogins');
lastLogins = obj.m_logins;
end
end
%% method which gets executed if login button gets pressed
function [ip,usr,pw] = loginCallback(obj,src,~)
% % % % can not access any members here??
% % % %
end
end
end

Answers (1)

Mohammad Sami
Mohammad Sami on 18 May 2021
You need to change this to a handle class instead of a value class.
classdef LoginDialog < handle

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!