Info

This question is closed. Reopen it to edit or answer.

Two axes GUI plot when i run it for the third time...

3 views (last 30 days)
Hi,
Got a problem with GUIDE. I created two GUI, one with two pushbuttons, first one to choose data, second to transfer data to an another GUI and open it. This second GUI is about to plot 2 contours of the data on two differents axes.
So in the first GUI i'm using xlsread to pick up data on excel and it works so i got finally 6 variables T2,V2,N2 ( for the first plot ) and T3,V3,N3 ( for the second plot ). I use global variable to transfer these variable to my puschbutton that call my second GUI ans it works too.
Then the second GUI openning and got error on the first plot and no plot display. Then I click another time on my puschbutton and it opended again but only with the first plot created and got error too but on the second plot. Then I click again on the button and finally, after 3 times, the two plots are display.
If someone know why...
Here is the puschbutton callback to pick up the data : ( just asking the user to choose the excel then type the differents cellrange )
global T2;
global V2;
global N2;
global T3;
global V3;
global N3;
startingFolder = 'C:\Users\moi\Documents\ISAT\4A\Stage FPT\Programme\';
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
fullExcelFileName2 = fullfile(folder, baseFileName);
defaultValue = 'Sheet1';
titleBar = 'Entrer le nom de la feuille 1';
userPrompt = 'Entrer le nom de la feuille 1';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end;
sheetName2 = strtrim(char(caUserInput));
defaultValueT = '';
titleBarT = 'Entrer les cellules T1';
userPromptT = 'Entrer les cellules T1';
caUserInputT = inputdlg(userPromptT, titleBarT, 1, {num2str(defaultValueT)});
if isempty(caUserInputT),return,end;
cellsRangeT2 = strtrim(char(caUserInputT));
defaultValueV = '';
titleBarV = 'Entrer les cellules V1';
userPromptV = 'Entrer les cellules V1';
caUserInputV = inputdlg(userPromptV, titleBarV, 1, {num2str(defaultValueV)});
if isempty(caUserInputV),return,end;
cellsRangeV2 = strtrim(char(caUserInputV));
defaultValueN = '';
titleBarN = 'Entrer les cellules N1';
userPromptN = 'Entrer les cellules N1';
caUserInputN = inputdlg(userPromptN, titleBarN, 1, {num2str(defaultValueN)});
if isempty(caUserInputN),return,end;
cellsRangeN2 = strtrim(char(caUserInputN));
T2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeT2);
V2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeV2);
N2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeN2);
startingFolder = 'C:\Users\moi\Documents\ISAT\4A\Stage FPT\Programme\';
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
fullExcelFileName3 = fullfile(folder, baseFileName)
defaultValue = 'Sheet1';
titleBar = 'Entrer le nom de la feuille 2';
userPrompt = 'Entrer le nom de la feuille 2';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end;
sheetName3 = strtrim(char(caUserInput))
defaultValueT = '';
titleBarT = 'Entrer les cellules T2';
userPromptT = 'Entrer les cellules T2';
caUserInputT = inputdlg(userPromptT, titleBarT, 1, {num2str(defaultValueT)});
if isempty(caUserInputT),return,end;
cellsRangeT3 = strtrim(char(caUserInputT))
defaultValueV = '';
titleBarV = 'Entrer les cellules V2';
userPromptV = 'Entrer les cellules V2';
caUserInputV = inputdlg(userPromptV, titleBarV, 1, {num2str(defaultValueV)});
if isempty(caUserInputV),return,end;
cellsRangeV3 = strtrim(char(caUserInputV))
defaultValueN = '';
titleBarN = 'Entrer les cellules N2';
userPromptN = 'Entrer les cellules N2';
caUserInputN = inputdlg(userPromptN, titleBarN, 1, {num2str(defaultValueN)});
if isempty(caUserInputN),return,end;
cellsRangeN3 = strtrim(char(caUserInputN))
T3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeT3);
V3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeV3);
N3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeN3);
Here is the callback for the puschbutton that call my second GUI...
handles = guidata(hObject);
guidata(hObject, handles);
global T2;
global V2;
global N2;
global T3;
global V3;
global N3;
compare(handles)
hf=findobj('Name','pageprincipale');
close(hf);
And here is the lines for the function that display the two plot of the second GUI...
function compare_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles=guidata(hObject);
axes(handles.axes1)
global T2;
global V2;
global N2;
global rangeY2;
global rangeX2;
global Z2;
global X2;
global Y2;
rangeY2=min(T2):55:max(T2);
rangeX2=min(V2):55:max(V2);
Z2 = griddata(V2,T2,N2,X2,Y2,'cubic');
[X2,Y2]=meshgrid(rangeX2,rangeY2);
rangeZ2=floor(min(N2)):0.1:ceil(max(N2));
contour(X2,Y2,Z2,[0 0.1 0.2 0.3 0.4 0.5 1 2 3 4 5 6 7 8 9],'showtext','on');
set(gca,'xlim',[800 2400],'xtick',800:100:2400);
set(gca,'ylim',[0 2500],'ytick',0:200:2500);
axes(handles.axes2)
global T3;
global V3;
global N3;
global rangeY3;
global rangeX3;
global Z3;
global X3;
global Y3;
rangeY3=min(T3):55:max(T3);
rangeX3=min(V3):55:max(V3);
Z3 = griddata(V3,T3,N3,X3,Y3,'cubic');
[X3,Y3]=meshgrid(rangeX3,rangeY3);
rangeZ3=floor(min(N3)):0.1:ceil(max(N3));
contour(X3,Y3,Z3,[0 0.1 0.2 0.3 0.4 0.5 1 2 3 4 5 6 7 8 9],'showtext','on');
set(gca,'xlim',[800 2400],'xtick',800:100:2400);
set(gca,'ylim',[0 2500],'ytick',0:200:2500);
guidata(hObject, handles);
And finaly the error I got when I push the button for the first time ( the second time is the same but replace the "2" by "3" ...
Error using contour (line 55)
Lengths of X and Y must match number of cols and rows in Z, respectively.
Error in compare_compare_OpeningFcn (line 43)
contour(X2,Y2,Z2,[0 0.1 0.2 0.3 0.4 0.5 1 2 3 4 5 6 7 8 9],'showtext','on');
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in compare (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in pageprincipale>pushbutton4_Callback (line 204)
compare(handles)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in pageprincipale (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)pageprincipale('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
It's like that the first time it doesn't took the variable so error, the second time it take correctly the variables for the first plot but not for the second and the third time it take all variables for the two plots...
If someone could give a try to give me a solution to this problem it would be appreciate.
Thank's.
Baptiste
  2 Comments
Adam
Adam on 15 Nov 2018
What is this code attempting to do?
The first two lines do effectively nothing, then all the global variables (which are a disaster waiting to happen anyway) declared which also seem to do nothing, so the whole code boils down to just passing the handles of this GUI to your compare function.
handles = guidata(hObject);
guidata(hObject, handles);
global T2;
global V2;
global N2;
global T3;
global V3;
global N3;
compare(handles)
hf=findobj('Name','pageprincipale');
close(hf);
Baptiste Tarbouriech
Baptiste Tarbouriech on 16 Nov 2018
Edited: Baptiste Tarbouriech on 16 Nov 2018
Thank's for answering.
The point of the code is to ask the user to choose 2 excel files, choose the range of data for the two excel files, then click on a button that open the second gui and plot 2 contour plot of the data choosed on two different axes. So yes the first Gui is about taking then transfering data and the second is about to draw. It's comparing the data of first excel file to the second excel file.
I am not confident with matlab and not even more with guide so it's complicated and I juste want to know why it plot after the third click on the button...
When I apply this code to only ONE excel file, there is no error and the plot is created immediatly on the second GUI, but with two files and two axes it's different.

Answers (2)

Baptiste Tarbouriech
Baptiste Tarbouriech on 16 Nov 2018
Edited: Baptiste Tarbouriech on 16 Nov 2018
So I made two new script with less information, more basics, juste to anderstand how to make this thing works... I decide to forget "global" to use handles.
So there is the code for the first GUI ( just 2 puschbuttons ) :
function varargout = test1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test1_OpeningFcn, ...
'gui_OutputFcn', @test1_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
function test1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = test1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
handles = guidata(hObject)
startingFolder = 'C:\Users\moi\Documents\ISAT\4A\Stage FPT\Programme\';
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
fullExcelFileName2 = fullfile(folder, baseFileName);
sheetName2 = 'OK'
cellsRangeT2 = 'AS136:AS248'
cellsRangeV2 = 'AP133:AP248'
cellsRangeN2 = 'AR136:AR248'
T2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeT2);
V2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeV2);
N2 = xlsread(fullExcelFileName2, sheetName2, cellsRangeN2);
handles.T22=T2;
handles.V22=V2;
handles.N22=N2;
startingFolder = 'C:\Users\moi\Documents\ISAT\4A\Stage FPT\Programme\';
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
return;
end
fullExcelFileName3 = fullfile(folder, baseFileName)
sheetName3 = 'OK'
cellsRangeT3 = 'AS136:AS248'
cellsRangeV3 = 'AP136:AP248'
cellsRangeN3 = 'AR136:AR248'
T3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeT3);
V3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeV3);
N3 = xlsread(fullExcelFileName3, sheetName3, cellsRangeN3);
handles.T33=T3;
handles.V33=V3;
handles.N33=N3;
guidata(hObject,handles)
function pushbutton2_Callback(hObject, eventdata, handles)
guidata(hObject,handles)
open(test2)
hf=findobj('Name','test1');
close(hf);
And there is the code for the second GUI ( just 2 axes ) :
function varargout = test2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test2_OpeningFcn, ...
'gui_OutputFcn', @test2_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
function test2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
T22 = [];
V22 = [];
N22 = [];
h = findobj('Tag','test1');
if ~isempty(h)
test1data = guidata(h);
if isfield(test1data,'T22')
T22 = test1data.T22;
end
test1data = guidata(h);
if isfield(test1data,'V22')
V22 = test1data.V22;
end
test1data = guidata(h);
if isfield(test1data,'N22')
N22 = test1data.N22;
end
end
axes(handles.axes1)
rangeY2=min(handles.T22):55:max(handles.T22);
rangeX2=min(handles.V22):55:max(handles.V22);
Z2 = griddata(handles.V22,handles.T22,handles.N22,handles.X22,handles.Y22,'cubic');
[X2,Y2]=meshgrid(rangeX2,rangeY2);
rangeZ2=floor(min(handles.N22)):0.1:ceil(max(handles.N22));
contour(X2,Y2,Z2,[0 0.1 0.2 0.3 0.4 0.5 1 2 3 4 5 6 7 8 9],'showtext','on');
set(gca,'xlim',[800 2400],'xtick',800:100:2400);
set(gca,'ylim',[0 2500],'ytick',0:200:2500);
axes(handles.axes2)
rangeY3=min(handles.T33):55:max(handles.T33);
rangeX3=min(handles.V33):55:max(handles.V33);
Z3 = griddata(handles.V33,handles.T33,handles.N33,handles.X33,handles.Y33,'cubic');
[X3,Y3]=meshgrid(rangeX3,rangeY3);
rangeZ3=floor(min(handles.N33)):0.1:ceil(max(handles.N33));
contour(X3,Y3,Z3,[0 0.1 0.2 0.3 0.4 0.5 1 2 3 4 5 6 7 8 9],'showtext','on');
set(gca,'xlim',[800 2400],'xtick',800:100:2400);
set(gca,'ylim',[0 2500],'ytick',0:200:2500);
guidata(hObject, handles);
function varargout = test2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
Then there is the error, I think I don't code correctly my second puschbutton...
Reference to non-existent field 'T22'.
Error in test2_test2_OpeningFcn (line 44)
rangeY2=min(handles.T22):55:max(handles.T22);
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in test2 (line 14)
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Error in test1>pushbutton2_Callback (line 82)
open(test2)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in test1 (line 16)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)test1('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Some help would be hugely appreciate...
Baptiste
  1 Comment
Jan
Jan on 16 Nov 2018
Please do not post parts of the question in the section for answers. See my answer, which is actually a comment to this message.

Jan
Jan on 16 Nov 2018
Edited: Jan on 16 Nov 2018
This seem to be trivial. After:
function test2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
T22 = [];
...
h = findobj('Tag','test1');
if ~isempty(h)
test1data = guidata(h);
if isfield(test1data,'T22')
T22 = test1data.T22;
end
end
axes(handles.axes1)
the field handles.T22 is not defined. If any object is found, which has the tag 'test1' (even in another GUI - is this really wanted?!), teh variable T22 is defined, but not handles.T22 . Then
rangeY2=min(handles.T22):55:max(handles.T22);
must fail with the show message.
Use the debugger to examine such problems:
dbstop if error
After typing this in the command window, Matlab stops when an error occurs. Then check the values of the variables. You will see that handles does not contain the expected field T22. handles.T22 is created inside the pushbutton1_Callback, but you request it in the OpeningFcn already, which is evaluated during the figure is created.
  3 Comments
Jan
Jan on 16 Nov 2018
If this line fails:
Z2 = griddata(V22,T22,N22,X2,Y2,'cubic');
set a breakpoint there and check the sizes of the used variables. I suggest another time to use the debugger by your own, because this is the right tool for finding out the reasons of errors.
Baptiste Tarbouriech
Baptiste Tarbouriech on 16 Nov 2018
I tried but it bring me into the code of the griddata function that i can't edit. I found another solution, I will pick up data, calculate, and pot, everything in the second gui... It works but it's simple

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!