- the 3 .m files and
- the example*.mat files, and
- 3 .fig files
How do I prevent my figures from duplicating?
17 views (last 30 days)
Show older comments
Pedro Guevara
on 28 Dec 2020
Commented: Pedro Guevara
on 3 Jan 2021
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
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Install Products 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!