MATLAB: How to move a rectangle when a button is pressed in app designer

app designerrectangle

I created a rectangle in the UIAxes window by using
tArea = rectangle(app.UIAxes, 'Position', [25 25 50 50], 'FaceColor', [0 1 .75 .3], 'EdgeColor', [0 1 .75 .3]);
but now I want to change its position when I click a button in the app. I wanted to just use
tArea.Position = [25 30 50 50];
but I think tArea is outside the scope of the buttonClicked function. I am not sure the best way to make tArea visible to the other function in app designer. Or is there a better way to do it altogether?

Best Answer

The easiest way is to create a public property in App designer. Go to Code View, in the top bar click the arrow under property and click Public Property. Name the property tArea and access it from any method using app.tArea. For example,
app.tArea = rectangle(app.UIAxes, 'Position', [25 25 50 50], 'FaceColor', [0 1 .75 .3], 'EdgeColor', [0 1 .75 .3]);
then to change
app.tArea.Position = [25 30 50 50];