Clear Filters
Clear Filters

Trying to delete the previous data/line from a next button using matlab Gui

2 views (last 30 days)
Hello everyone,
i have a little problem. I was able to code a "Next button" using app designer that have as aim to draw lines. I had the data coming from excel. I am able to draw each line by clicking on the next button. The only problem is that the previous data/line is not deleted. So instead of having one line, i will have 2 or more depending on how many times i click on the next button.
To illustrate it more, you can see the attached document. I am expecting to have just one LINE/BAND
the Next button code is below
uiconfirm(app.UIFigure,'Are You sure?','Confirm Close',...
app.exp_counter = app.exp_counter + 1;
v_or_h = app.v_or_h_array(app.exp_counter);
app.v_thick1 = app.v_thickness_1(app.exp_counter);
app.v_thick2 = app.v_thickness_2(app.exp_counter);
app.h_thick1 = app.h_thickness_1(app.exp_counter);
app.h_thick2 = app.h_thickness_2(app.exp_counter);
%Vertical line
if v_or_h == 0
app.region1 = patch(app.UIAxes, ...
[app.v_thick1 app.v_thick2 app.v_thick2 app.v_thick1], ...
[-10 -10 10 10],'r', ...
'LineWidth',0.01, ...
%Horizontal line
elseif v_or_h == 1
app.region1 = patch(app.UIAxes,[-10 10 10 -10], ...
[app.h_thick1 app.h_thick1 app.h_thick2 app.h_thick2], ...
'LineWidth',0.01, ...
Walter Roberson
Walter Roberson on 13 Oct 2023
In the code, app.UIFigure is the property name inside app that has been given to the handle of a uifigure object, and app.UIAxes is the property name inside app that has been given to the handle of a uiaxes object (that is presumably inside UIFigure)
Each UIFigure has a property which records CurrentPoint (current point is recorded independently for each different figure). The units for the position defaults to pixels . This code assumes that whatever units is being used for the uifigure, the same units are being used for the axes.
CurrentPoint is in the order x and then y.
Each UIAxes has a property Position which records the location of the axes. The units for the position defaults to pixels. It is a vector which has the order bottom-left x, bottom-left y, width, height . So sum(app.UIAxes.Position([1,3])) is extracting the x of the bottom left and adding the width to determine where the right hand side is, and sum(app.UIAxes.Position([2,4])) is calculating where the top is. The code is therefore determining whether the current x position is within the left and right limits of the axes, and the current y position is within the bottom and top limits of the axes.
The code could be more robust. It could, for example, look at the figure CurrentAxes property instead of assuming a particular axes. It could avoid the assumption that the axes and the figure have the same Units.
The current source for overobj() also assumes that the units are pixels, but if you look at the source code it has the proper units conversion inside it, just commented out.
B. Young
B. Young on 13 Oct 2023
Thank you, Walter, so much for your explanation and the document you recommended!!!! now I know what it means and I solved my problem just now!!

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Oct 2021
@Franck paulin Ludovig pehn Mayo - If you only want one patch object to appear, then you will need to delete the old one before creating the new one since you overwrite the "old" patch handle with the new one:
app.region1 = patch(app.UIAxes, ...
You would need a line of code like
if ishandle(app.region1)
called before you draw the new patch. Note that I'm assuming that the app object has an attribute named region1. If non such attribute exists, then you will need to handle that case.
Geoff Hayes
Geoff Hayes on 21 Oct 2021
@Franck paulin Ludovig pehn Mayo - are you just writing a string to the text file? if so, then just append to the file rather than write and discard the existing contents. Please see file type access and so you would change your code from
fileID = fopen('exp.txt2','w');
fileID = fopen('exp.txt2','a');
Note that you should add a check in your code to make sure the fileID is valid before trying to write data to the file (i.e. fileID > 0).
Also, you may want to write to a new line each time (or not?) so add the \n to your fprintf

Sign in to comment.

More Answers (0)


Find more on App Building in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!