App Designer: I identified my variables in one function, but it is not carrying over to the next function, so it informs me that my Variable (specifically P1) is undefined.

10 visualizaciones (últimos 30 días)
Hello, I have very little experince in Matlab, especially the app designer. I would love some assistance in solving the problem where I can not transfer over my variables from my StartButtonPushed function to my HitButtonPushed function.
classd
ef V1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
HitButton matlab.ui.control.Button
StandButton matlab.ui.control.Button
DoubleDownButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
P1 = zeros(1,10); %Create a zeros row so that values can be replaced
P2 = zeros(1,10);
H = zeros(1,10);
deck = randperm(52); %Creates a vector of random numbers from 1 to 52 without repeats
P1(1) = deck(1); %Deal player 1 the first card
P2(2) = deck(2); %Deal player 2 the second card
H(1) = deck(3); %Deal house the third card
P1(2) = deck(4);% Deal player 1 the fourth card
P2(2) = deck(5); %Deal player 2 the fifth card
H(2) = deck(6); %Deal house the sixth card
%totalCardsDrawn = length(P1) +length(P2) +length(H)
totalCardsDrawn = nnz(P1) +nnz(P2) + nnz(H);
cardVal_1 = P1(1); %This will take the first card delt to player 1 and assign it a value
switch cardVal_1
case {1 2 3 4 }
cardVal_1 = 1;
case {5 6 7 8}
cardVal_1 = 2;
case {9 10 11 12}
cardVal_1 = 3;
case {13 14 15 16}
cardVal_1 = 4;
case {17 18 19 20}
cardVal_1 = 5;
case {21 22 23 24}
cardVal_1 = 6;
case {25 26 27 28}
cardVal_1 = 7;
case {29 30 31 32}
cardVal_1 = 8;
case {33 34 35 36}
cardVal_1 = 9;
otherwise
cardVal_1 = 10;
end
P1(1) = cardVal_1;
cardVal_2 = P1(2); %This will take the second card delt to player 1 and assign it a value
switch cardVal_2
case {1 2 3 4 }
cardVal_2 = 1;
case {5 6 7 8}
cardVal_2 = 2;
case {9 10 11 12}
cardVal_2 = 3;
case {13 14 15 16}
cardVal_2 = 4;
case {17 18 19 20}
cardVal_2 = 5;
case {21 22 23 24}
cardVal_2 = 6;
case {25 26 27 28}
cardVal_2 = 7;
case {29 30 31 32}
cardVal_2 = 8;
case {33 34 35 36}
cardVal_2 = 9;
otherwise
cardVal_2 = 10 ;
end
P1(2) = cardVal_2
cardVal_3 = P2(1); %This will take the first card delt to player 2 and give it a value
switch cardVal_3
case {1 2 3 4 }
cardVal_3 = 1;
case {5 6 7 8}
cardVal_3 = 2;
case {9 10 11 12}
cardVal_3 = 3;
case {13 14 15 16}
cardVal_3 = 4;
case {17 18 19 20}
cardVal_3 = 5;
case {21 22 23 24}
cardVal_3 = 6;
case {25 26 27 28}
cardVal_3 = 7;
case {29 30 31 32}
cardVal_3 = 8;
case {33 34 35 36}
cardVal_3 = 9;
otherwise
cardVal_3 = 10 ;
end
P2(1) = cardVal_3;
cardVal_4 = P2(2); %This will take the second card delt to player 2 and give it a value
switch cardVal_4
case {1 2 3 4 }
cardVal_4 = 1;
case {5 6 7 8}
cardVal_4 = 2;
case {9 10 11 12}
cardVal_4 = 3;
case {13 14 15 16}
cardVal_4 = 4;
case {17 18 19 20}
cardVal_4 = 5;
case {21 22 23 24}
cardVal_4 = 6;
case {25 26 27 28}
cardVal_4 = 7;
case {29 30 31 32}
cardVal_4 = 8;
case {33 34 35 36}
cardVal_4 = 9;
otherwise
cardVal_4 = 10 ;
end
P2(2) = cardVal_4;
%P2_card = [cardVal_3 cardVal_4]
cardVal_5 = H(1); %This will take the first card delt to the house and give it a value
switch cardVal_5
case {1 2 3 4 }
cardVal_5 = 1;
case {5 6 7 8}
cardVal_5 = 2;
case {9 10 11 12}
cardVal_5 = 3;
case {13 14 15 16}
cardVal_5 = 4;
case {17 18 19 20}
cardVal_5 = 5;
case {21 22 23 24}
cardVal_5 = 6;
case {25 26 27 28}
cardVal_5 = 7;
case {29 30 31 32}
cardVal_5 = 8;
case {33 34 35 36}
cardVal_5 = 9;
otherwise
cardVal_5 = 10 ;
end
H(1) = cardVal_5;
cardVal_6 = H(2); %This will take the second card delt to the house and give it a value
switch cardVal_6
case {1 2 3 4 }
cardVal_6 = 1;
case {5 6 7 8}
cardVal_6 = 2;
case {9 10 11 12}
cardVal_6 = 3;
case {13 14 15 16}
cardVal_6 = 4;
case {17 18 19 20}
cardVal_6 = 5;
case {21 22 23 24}
cardVal_6 = 6;
case {25 26 27 28}
cardVal_6 = 7;
case {29 30 31 32}
cardVal_6 = 8;
case {33 34 35 36}
cardVal_6 = 9;
otherwise
cardVal_6 = 10 ;
end
H(2) = cardVal_6;
end
% Button pushed function: HitButton
function HitButtonPushed(app, event)
totalCardsDrawn = nnz(P1) +nnz(P2) + nnz(H)
drawCard = deck(totalCardsDrawn+ 1);
%function addCardVal=casefunc(drawCard)
switch drawCard
case {1 2 3 4 }
addCardVal = 1;
case {5 6 7 8}
addCardVal = 2;
case {9 10 11 12}
addCardVal = 3;
case {13 14 15 16}
addCardVal = 4;
case {17 18 19 20}
addCardVal = 5;
case {21 22 23 24}
addCardVal = 6;
case {25 26 27 28}
addCardVal = 7;
case {29 30 31 32}
addCardVal = 8;
case {33 34 35 36}
addCardVal = 9;
otherwise
addCardVal = 10 ;
end
lengthP1 = nnz(P1);
newLength_P1 = lengthP1 +1
P1(newLength_P1) = addCardVal
end
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 711 496];
app.UIFigure.Name = 'UI Figure';
% Create HitButton
app.HitButton = uibutton(app.UIFigure, 'push');
app.HitButton.ButtonPushedFcn = createCallbackFcn(app, @HitButtonPushed, true);
app.HitButton.Position = [383 120 100 22];
app.HitButton.Text = 'Hit';
% Create StandButton
app.StandButton = uibutton(app.UIFigure, 'push');
app.StandButton.Position = [497 120 100 22];
app.StandButton.Text = 'Stand';
% Create DoubleDownButton
app.DoubleDownButton = uibutton(app.UIFigure, 'push');
app.DoubleDownButton.Position = [611 120 100 22];
app.DoubleDownButton.Text = 'Double Down';
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [284 289 141 41];
app.StartButton.Text = 'Start';
% 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 = V1
% 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
  1 comentario
Jason Grosser
Jason Grosser el 28 de Nov. de 2019
I am new to app designer as well and having exactly the same problem. Variables defined in one function are not visible to another function. I want to make all my variables global.
How do I do that???

Iniciar sesión para comentar.

Respuestas (1)

Melissa Williams
Melissa Williams el 3 de Dic. de 2019
Hi,
To make your variables accessible to all functions you need to make them an app property. To do this, in Code View, on the Editor tab of the toolstrip, in the Insert Section, select add Property.
addProp.png
You will see some code like this:
properties (Access = private)
Property % Description
end
Rename "Property" to be the name of your variable and whenever you want to access it, use app.NewPropertyName.
  1 comentario
Danya Pradeep Kumar
Danya Pradeep Kumar el 1 de Jul. de 2020
Hi Melissa,
This answer solved the problem I was facing in my app to use variables defined in one function in another. Thank you!
Danya

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by