Only 1st of 2 axes in GUIDE updates

3 ビュー (過去 30 日間)
saurosaum
saurosaum 2017 年 8 月 4 日
コメント済み: saurosaum 2017 年 8 月 4 日
Hi there, I have a Matlab GUI where I load a .mat file with variables from a program using a button, enter a variable name in a edit text box and then hit a second button to plot figures on 2 axes. The .mat file is same for an experiment whereas the entered variable in the text box changes. When I do this, only the axes that is written first updates. The second axis remains stuck at the plot from the last entered variable. For example in the code written here, only axes2 updates. I have tried using cla() and GUIUpdate but to no avail. Many thanks in advance for your help!
Here is the code:
function varargout = MSDBPlot(varargin)
% MSDBPLOT MATLAB code for MSDBPlot.fig
% MSDBPLOT, by itself, creates a new MSDBPLOT or raises the existing
% singleton*.
%
% H = MSDBPLOT returns the handle to a new MSDBPLOT or the handle to
% the existing singleton*.
%
% MSDBPLOT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MSDBPLOT.M with the given input arguments.
%
% MSDBPLOT('Property','Value',...) creates a new MSDBPLOT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MSDBPlot_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MSDBPlot_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 MSDBPlot
% Last Modified by GUIDE v2.5 03-Aug-2017 21:07:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MSDBPlot_OpeningFcn, ...
'gui_OutputFcn', @MSDBPlot_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 MSDBPlot is made visible.
function MSDBPlot_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 MSDBPlot (see VARARGIN)
% Choose default command line output for MSDBPlot
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MSDBPlot wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MSDBPlot_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,~]=uigetfile('*.mat','Get File')
handles = load(FileName)
guidata(hObject,handles)
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
handles.geneName = get(hObject,'String');
guidata(hObject,handles);
disp(handles.geneName);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
userInput = handles.geneName;
disp(userInput)
for i = 1:length(handles.prot)
%%Do several calculations on the loaded variables
end
axes(findobj('Tag','axes1'));
hist(varYY1,XX);
title('varYY1');
axes(findobj('Tag','axes2'));
hist(varYY2,XX);
title('varYY2');

採用された回答

Jan
Jan 2017 年 8 月 4 日
編集済み: Jan 2017 年 8 月 4 日
function pushbutton1_Callback(hObject, eventdata, handles)
[FileName,~]=uigetfile('*.mat','Get File')
handles = load(FileName)
guidata(hObject,handles)
This is a really bad idea. Do not overwrite the handles struct.
Consider the path name also, because a user can select another folder in the uigetfile dialog:
[FileName, FilePath] = uigetfile('*.mat', 'Get File');
if ~ischar(FileName)
disp('User canceled the file selection');
return;
end
handles.FileData = load(fullfile(PathName, FileName))
guidata(hObject, handles);
This is a questionable idea also:
function edit1_Callback(hObject, eventdata, handles)
handles.geneName = get(hObject,'String');
guidata(hObject,handles);
Remember that the "handles" in the input argument is the contents of the struct conserved at the time, when the callback has been defined - or did GUIDE use a smart mechanism to update the input now? Safer and/or perhaps the only valid method:
function edit1_Callback(hObject, eventdata, handlesFronInput)
handles = guidata(hObject); % Current value from the figure
handles.geneName = get(hObject,'String');
Searching for tags can be time consuming and tags need not be unique, e.g. if you open multiple instances of the dialog. Prefer using the handles:
% axes(findobj('Tag','axes1'));
% hist(varYY1,XX);
% title('varYY1');
hist(handkles.axes2, varYY1, XX);
title(handles.axes2, 'varYY1');
This is safer also, when the user clicks in GUI elements during the code runs. Setting the active axes by the axes() command is overridden, when the user clicks on another axes.
  1 件のコメント
saurosaum
saurosaum 2017 年 8 月 4 日
Thank you, Jan for not only solving the posted problem but also suggesting key improvements to my code. I really appreciate it!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by