MATLAB Answers

How do I prevent my figures from duplicating?

286 views (last 30 days)
Good day.
I have the following problem. I have 1 project with 6 files (3 of type .m and 3 of type .fig, each .m corresponds to a .fig). The problem is the following: - In the file "untitled .m" I have a function (untitled_OpeningFcn) that allows to graph (just run the untitled.m) in the figure "untitled.fig", in turn in the "untitled.fig" I have a button that allows me to open "untitled3.fig" where I can add or remove coordinate points that will be added or removed from my graph; however when I remove or add points (using buttons in untitled3.fig) and call the file "untitled.m" to update the graph, a new figure "untitled.fig" is generated updated with the points but duplicated.
I wish the figure "untitled.fig" is updated with the new points, but that this figure is not duplicated. I apologize in advance if I am not fully fluent in the MATLAB language, as I am learning to program in it. I leave some lines of code for your better understanding.
  • untitled.m code:
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 03-Jul-2020 22:04:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
if isempty( getappdata (0, 'evalue' ) )
appdata = get(0,'ApplicationData')
fns = fieldnames(appdata);
for ii = 1:numel(fns)
rmappdata(0,fns{ii});
end
appdata = get(0,'ApplicationData') %make sure it's gone
xxx = getappdata (0, 'evalue' )
else
xxx = getappdata (0, 'evalue' );
end
%%%%%%%%%%%%%%
if exist('example.mat') ~= 0
%%%%%% Points 3D
hold off
m = matfile('example.mat');
Data_nodos = (m.T_E_MAT);
x= Data_nodos(:,2);
y= Data_nodos(:,3);
z= Data_nodos(:,4);
scatter3(x,y,z,'Parent', handles.Grafic);
figyyy = gca;
X_min= min( Data_nodos ( :,2 ) );
X_max= max( Data_nodos ( :,2 ) );
Y_min= min( Data_nodos ( :,3 ) );
Y_max= max( Data_nodos ( :,3 ) );
Z_min= min( Data_nodos ( :,4 ) );
Z_max= max( Data_nodos ( :,4 ) );
X_min= min ( X_min );
X_max= max ( X_max );
Y_min= min ( Y_min );
Y_max= max ( Y_max );
Z_min= min ( Z_min );
Z_max= max ( Z_max );
%%%%%% Lines 3D
hold off
m = matfile('example2.mat');
Data_Elementos = (m.T_E_MAT2);
Data_nodos = Data_nodos % Matriz de nodos
if ( isempty(Data_Elementos) == 0 ) % Entra si "Data_Elementos" tiene datos
if isempty(xxx)==1 % Si xxx esta vacio (nudo borrado)
else % Si xxx no esta vacio (nudo no borrado)
Bus_N_I = find( Data_Elementos ( : , 2) == xxx );
Bus_N_F = find( Data_Elementos ( : , 3) == xxx );
Data_Elementos( [Bus_N_I,Bus_N_F] , : ) = []
end
[numRows,numCols] = size(Data_Elementos)
for i=1:numRows
Pos = ismember ( Data_nodos (:,1) , Data_Elementos ( i, 2:3 ) )
Pos = find(Pos == 1);
P1(i,1:3) = Data_nodos ( Pos(1,1) , 2:4 )
P2(i,1:3) = Data_nodos ( Pos(2,1) , 2:4 )
gca;
plot3( handles.Grafic, [ P1(i,1) ,P2(i,1)] , [ P1(i,2), P2(i,2) ] , [ P1(i,3), P2(i,3) ] )
hold on
end
text(x,y,z, cellstr ( num2str ( Data_nodos ( 1:end , 1 ) ) ), 'VerticalAlignment','bottom','HorizontalAlignment','right' )
xlim([X_min-2 X_max+2]);
ylim([Y_min-2 Y_max+2]);
zlim([Z_min-2 Z_max+2]);
end
end
  • untitled3.m code (Delete button):
function Obj_Del_Callback(hObject, eventdata, handles)
Var_Del_Ele = get(handles.Obj_Pop_Del,'value'); % Elemento seleccionado en "Obj_Pop_Del"
counter= get (handles.Obj_Add, 'UserData' ) ;
set(handles.Obj_Pop_Del, 'Value', counter - 1);
Ele_pop = str2num ( get(handles.Obj_Pop_Del,'string') );
Ele_pop = Ele_pop'
Ele_pop(Var_Del_Ele) = [];
Ele_pop = [ Ele_pop(1,1:Var_Del_Ele-1) , Ele_pop(1,Var_Del_Ele:end) - 1 ];
set(handles.Obj_Pop_Del,'string',Ele_pop); %%% Sobre-Escribo sobre Pop up - (List_Contor)
m = matfile('example2.mat','Writable',true)
data = (m.T_E_MAT2);
data(Var_Del_Ele, :) = [];
data(:, 1) = [];
data = [ Ele_pop' , data ]
m.T_E_MAT2 = data; %%% matObj.T_E_MAT2 = data
set(handles.Obj_tabla_Ele,'Data',data);
Var_Del_point_cont = get(handles.Obj_Add,'UserData');
set(handles.Obj_Add, 'UserData', Var_Del_point_cont - 1);
untitled; %%% Se va al OpeningFcn de "untitled";
%figure(untitled3) %%% Mantiene .fig "untitled3" por encima de las demas ventanas;
  5 Comments
Pedro Guevara
Pedro Guevara on 3 Jan 2021
Thank you very much for your help. I managed to solve it using the function "Run" and "Close" to my Guide. It was easier than I thought, but it took me a long time to know what was the necessary function to achieve it. I leave the solution below for whoever wants to see it.

Sign in to comment.

Accepted Answer

Aghamarsh Varanasi
Aghamarsh Varanasi on 31 Dec 2020
Edited: Aghamarsh Varanasi on 31 Dec 2020
Hi,
The figure 'untitiled.fig' is duplicating as a result of 'untitled' function call from the ‘Obj_Add_Callback’ function in the ‘untitled.m’ file. Whenever 'untitled' is called, a new GUI figure is created for ‘untitled.fig’.
As a work around, you can send the updated data from 'untitled3' to 'untitled' and then update the figure.
For more information on passing information between two apps, refer to this documentation. You can click on 'Try It In MATLAB' to run the example.
Hope this helps
  2 Comments
Pedro Guevara
Pedro Guevara on 3 Jan 2021
Thank you very much for your help. I managed to solve it using the function "Run" and "Close" to my Guide. It was easier than I thought, but it took me a long time to know what was the necessary function to achieve it. I leave The Codes in case someone is interested.
  • untitled3:
function varargout = untitled3(varargin)
% UNTITLED3 MATLAB code for untitled3.fig
% UNTITLED3, by itself, creates a new UNTITLED3 or raises the existing
% singleton*.
%
% H = UNTITLED3 returns the handle to a new UNTITLED3 or the handle to
% the existing singleton*.
%
% UNTITLED3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED3.M with the given input arguments.
%
% UNTITLED3('Property','Value',...) creates a new UNTITLED3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled3
% Last Modified by GUIDE v2.5 27-May-2020 11:51:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled3_OpeningFcn, ...
'gui_OutputFcn', @untitled3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% --- Executes just before untitled3 is made visible.
function untitled3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled3 (see VARARGIN)
% Choose default command line output for untitled3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set (handles.Obj_Add, 'UserData' , 0);
if exist('example.mat') ~= 0
matObj = matfile('example.mat','Writable',true )
A = matObj.T_E_MAT; %%% Leo excel
set(handles.Obj_pop_NI, 'string', A(:,1) )
set(handles.Obj_pop_NF, 'string', A(:,1) )
if exist('example2.mat') ~= 0
matObj = matfile('example2.mat','Writable',true )
A = matObj.T_E_MAT2; %%% Leo excel
set(handles.Obj_Pop_Del, 'string', A(:,1) )
set(handles.Obj_tabla_Ele, 'Data', A )
set (handles.Obj_Add, 'UserData' , length ( A(:,1) ) ) ;
end
end
% --- Executes on button press in Obj_Add.
function Obj_Add_Callback(hObject, eventdata, handles)
% hObject handle to Obj_Add (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
m = matfile('example.mat');
oldData = m.T_E_MAT; %%% Leo excel
Var_I = get(handles.Obj_pop_NI,'Value')
Var_F = get(handles.Obj_pop_NF,'Value')
Buscar_elemento = [Var_I,Var_F]'
Buscar_elemento = ismember( oldData (:,1) , Buscar_elemento )
Bus_N = find( Buscar_elemento == 1 );
if ( (oldData ( Bus_N (1,1) , 2 ) == oldData ( Bus_N (2,1) , 2 ) && oldData ( Bus_N (1,1) , 3 ) == oldData ( Bus_N (2,1) , 3 )) ...
|| ( oldData (Bus_N (1,1) , 4 ) == oldData ( Bus_N (2,1) , 4 ) ) )
counter = get (hObject, 'UserData' ) + 1;
if counter == 1
d = [counter,Var_I,Var_F];
set(handles.Obj_tabla_Ele,'Data',d)
else
d = [counter,Var_I,Var_F];
m = matfile('example2.mat');
oldData = m.T_E_MAT2; %%% Leo excel
dd=[oldData;d];
set(handles.Obj_tabla_Ele,'Data',dd)
end
tabla_excel=get(handles.Obj_tabla_Ele,'data');
set (hObject, 'UserData' , counter);
T_E_MAT2 = tabla_excel; save example2.mat T_E_MAT2 -v7.3; clear T_E_MAT2
set(handles.Obj_Pop_Del, 'string', tabla_excel(:,1) )
untitled; %%% Se va al OpeningFcn de "untitled"
figure(untitled3) %%% Mantiene .fig "untitled3" por encima de las demas ventanas
end
% --- Executes on button press in Obj_Del.
function Obj_Del_Callback(hObject, eventdata, handles)
Var_Del_Ele = get(handles.Obj_Pop_Del,'value'); % Elemento seleccionado en "Obj_Pop_Del"
counter= get (handles.Obj_Add, 'UserData' ) ;
set(handles.Obj_Pop_Del, 'Value', counter - 1);
Ele_pop = str2num ( get(handles.Obj_Pop_Del,'string') );
Ele_pop = Ele_pop'
Ele_pop(Var_Del_Ele) = [];
Ele_pop = [ Ele_pop(1,1:Var_Del_Ele-1) , Ele_pop(1,Var_Del_Ele:end) - 1 ];
set(handles.Obj_Pop_Del,'string',Ele_pop); %%% Sobre-Escribo sobre Pop up - (List_Contor)
m = matfile('example2.mat','Writable',true)
data = (m.T_E_MAT2);
data(Var_Del_Ele, :) = [];
data(:, 1) = [];
data = [ Ele_pop' , data ]
m.T_E_MAT2 = data; %%% matObj.T_E_MAT2 = data
set(handles.Obj_tabla_Ele,'Data',data);
Var_Del_point_cont = get(handles.Obj_Add,'UserData');
set(handles.Obj_Add, 'UserData', Var_Del_point_cont - 1);
%These are the lines of code that allowed me to solve the problem.
close ('Propiedades');
close ('Elementos');
run untitled;
run untitled;
untitled3;

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by