GUI stops after initialization code

The GUI was running. Then I added another push button in GUIDE. After that I tried to run the GUI again, but it doesnt work anymore. I figured out, that its only executing until the end of the initialization code. Other breakpoints are not even reached. Restarting Matlab doesnt help either. What could be the problem her?

11 Commenti

I'd bet that an error message is generated when the GUI stops initializing and I'd bet there was an error message when you were compiling the GUI after adding the problematic pushbutton.
Such problems occur when saving updates in GUIDE while the m-file contains breaks. If this sounds like what you're dealing with, attach the fig and m file and I can take a look at it. I've fixed this problem before but I haven't memorized the steps.
What code do you expect to be running as well? Have the callbacks been changed?
Also note that GUIDE is probably not the best tool for anything that is slightly more complex than two buttons and an axes.
Thank you for the fast reaction!
No there was no Error in the Command Window. Thats the problem. And I just rebuild the GUI and added the code again. And the same problem occured. My Code is quite long but here it is:
function varargout = GUI_start(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_start_OpeningFcn, ...
'gui_OutputFcn', @GUI_start_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 GUI_start is made visible.
function GUI_start_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject; % Nicht löschen gehört dazu
guidata(hObject, handles); % Nicht löschen gehört dazu
% --- Outputs from this function are returned to the command line.
function varargout = GUI_start_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output; % Nicht löschen gehört dazu
% --- Executes when chosenfigure1 is resized.
function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject handle to chosenfigure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function startbutton_Callback(hObject, eventdata, handles)
set(groot,'defaultFigureVisible','off'); % verhindert, dass neue Grafiken aufgehen
[Ausgabe, Simulation_Gesamt]= Aufruf_Parfor_70_Haushalte_1s_Verzug_GUI(0,handles.prog);
% handles.Simulation_Gesamt=Simulation_Gesamt; % Zahl für Prognose als allgemeine Variable definieren
handles.output= {Ausgabe, Simulation_Gesamt}; % sollte theoretisch die Variablen im Workspace erscheinen lassen
guidata(hObject,handles);
handles.resulttable.Data= {'E_Netz_bezogen';'E_Netz_ein';'E_PV';'E_Last';'E_Verlust_AR';'E_Verlust_WR';'E_Verlust_BWR'; 'E_Verlust_Batt'; 'Vollzyklen'; 'Summe Energie';'P_Bezug_max';'Leistungspreis';'Autarkiegrad'; 'Eigenverbrauchsquote'; 'KWBH_r';'Reduktion_Netzbezug' };
sz=size(handles.resulttable.Data); % Länge der Ergebnistabelle bestimmen
fields_erg_struct=fieldnames(Simulation_Gesamt); % Ein Array erzeugen in dem die Feldernamen des Ergebnis-Struct-Arrays gespeichert sind
for i =1:sz(1,1)
if ~isempty(find(contains(fields_erg_struct,handles.resulttable.Data(i,1)))) % Überprüfen ob einfach so in Liste vorhanden
index = find(contains(fields_erg_struct,handles.resulttable.Data(i,1))); % Finden in welcher Zeile das Ergebnis steht
handles.resulttable.Data(i,2)= {Simulation_Gesamt.(fields_erg_struct{index})(end)}; % Eintragen der Zahl in Tabelle
else %Problemfälle bahandeln
ergname=handles.resulttable.Data(i,1);
if ergname== "E_Netz_bezogen"
index = find(contains(fields_erg_struct,"E_Netz_bez_r"));
handles.resulttable.Data(i,2)= {Simulation_Gesamt.(fields_erg_struct{index})(end)};
end
if ergname== "Vollzyklen"
index = find(contains(fields_erg_struct,"counter_reaktiv_r"));
handles.resulttable.Data(i,2)= {Simulation_Gesamt.(fields_erg_struct{index})(end)};
end
if ergname== "Summe Energie"
handles.resulttable.Data(i,2)= {0}; % Ist total falsch aber dafür brauche ich auch die Parameter
end
if ergname== "P_Bezug_max"
index = find(contains(fields_erg_struct,"P_Netz_r"));
handles.resulttable.Data(i,2)= {max(Simulation_Gesamt.(fields_erg_struct{index}))};
end
if ergname== "Leistungspreis"
index = find(contains(fields_erg_struct,"P_Netz_r"));
handles.resulttable.Data(i,2)= {0.1*max(Simulation_Gesamt.(fields_erg_struct{index}))}; % Wenn Parameter vorhanden noch ändern anstatt 0.1
end
if ergname== "Reduktion_Netzbezug"
handles.resulttable.Data(i,2)= {100*(min(Ausgabe{1,1}.Netz_mean)/min(Ausgabe{1,1}.Netz_mean_roh))};
end
%msgbox(ergname)
end
end
% --- Executes on selection change in chosenfigure2.
function chosenfigure2_Callback(hObject, eventdata, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns chosenfigure2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from chosenfigure2
% --- Executes during object creation, after setting all properties.
function chosenfigure2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in chosenfigure1.
function chosenfigure1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function chosenfigure1_CreateFcn(hObject, eventdata, handles)
% hObject handle to chosenfigure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in loaddata.
function loaddata_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function loaddata_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in pvdata.
function pvdata_Callback(hObject, eventdata, handles)
% hObject handle to pvdata (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Anzahl der Dateien, die PV Daten enthalten
% contents = cellstr(get(hObject,'String')); % Inhalt des Drop-Down-Menüs
% --- Executes during object creation, after setting all properties.
function pvdata_CreateFcn(hObject, eventdata, handles)
% hObject handle to pvdata (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 selection change in prognose.
function prognose_Callback(hObject, eventdata, handles)
% hObject handle to prognose (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
prog = get(hObject,'Value')-1; % Ausgewählte Prognose Abgreifen
switch prog % Ausgabe Definieren
case 1
ausgabe = "ideal";
case 2
ausgabe = "PV-Direkt, Last-Prototyp";
case 3
ausgabe = "PV-Tagespersistenz, Last-Wochentagpersistenz";
otherwise
ausgabe = "ERROR";
end
set(handles.prognoseanzeige, 'String', ausgabe); % Ausgabe anzeigen
handles.prog=prog; % Zahl für Prognose als allgemeine Variable definieren
guidata(hObject,handles);
% Hints: contents = cellstr(get(hObject,'String')) returns prognose contents as cell array
% contents{get(hObject,'Value')} returns selected item from prognose
% --- Executes during object creation, after setting all properties.
function prognose_CreateFcn(hObject, eventdata, handles)
% hObject handle to prognose (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
Laura F
Laura F il 4 Ott 2019
Modificato: Laura F il 4 Ott 2019
And everything that is displayed in the Command Window is:
>> GUI_start
>>
The .m file is not even executing the Opening Function. Normally it should also run the Opening Function. Then the GUI-Window should open. And the the rest wold work. I'm not sure if there should be a create function and a callback function for every element that i created in the GUIDE. Because that wasn't the case when I first created the GUI via GUIDE.
I quickly glanced at the initialization code, opening function, and output function and there are no obvious red flags. Unfortunately we can't run the GUI without the figure file so if you attach the m file and the fig file, we can open the hood and check the engine.
Okay Thank you very much!
BTW, I second Rik's persistence in minimizing the use of GUIDE. I think drag-n-drop construction of GUIs is a useful tool, especially for beginners, but it comes with limitations and, in the case of GUIDE, bugs, if not used properly. Unlike drag-n-drop methods, building a GUI from the bottom-up by using uicontrol() or the uifigure version, requires more time during development but may save time in the long run by avoiding problems that are difficult to trace. It also gives you more control and background knowledge of object properties and such.
Okay good hint for the future. I'll keep that in mind, if this problem couldnt be solved.
I've got a solution that I'll describe in an answer below. The solution is easy but I'm going to take a short bit of time to explain the problem so bear with me.... check back in a few minutes.
Thank you so much!
Well Adam, your encouragment (and the fact that there now is a huge banner that GUIDE will be removed) pushed me over the egde, so I now posted a thread about this. Please feel free to weigh in.

Accedi per commentare.

 Risposta accettata

Adam Danz
Adam Danz il 4 Ott 2019
Modificato: Adam Danz il 26 Dic 2019
The problem
The "visible" property of your GUI figure was set to "off" which means the GUI was initialized properly, you just couldn't see it. As evidence, run your current version of the GUI that does not display the figure and then run this line of code which searches for figures even if they are invisible.
findall(0,'type','figure')
The solution
If you're working with Matlab r2019a or earlier, follow these steps.
  1. Remove any breaks in your GUI code and close the m-file.
  2. Open the GUI in GUIDE: guide('GUI_start')
  3. right-click the GUI background and select Property Inspector
  4. scroll down to "Visible" and check "on"
  5. Save the GUI in GUIDE and close the GUIDE window
  6. Now when you open your GUI it should be visible.
If you're working with Matlab r2019b, the problem is a bit deeper. Starting in r2019b an annotationPane handle is included in GUIDE GUIs and this handle was not present in GUIDE GUIs prior to r2019b. I brought this to the attention of the forum last week in this post. If you follow the instructions above in r2019b, you'll get an error as soon as you follow step #4 (selecting the Visible checkbox). The error is associated with the AnnotationPane class, shown below. If you're working with r2019b, instead of following the instructions above, you can just download the attachments which are your GUI files after I've completed the 6 steps above in r2019a. Alternatively, you can try the patch described in the first solution here.
[Update 26-Dec-2019]: Update #3 for r2019b fixes this problem: https://www.mathworks.com/support/bugreports/2083859

2 Commenti

Thank you very much. This worked.
In case you're working with your GUI in r2019b, I wrote a patch to try to solve the problem of accessing the GUI while avoiding the bug in the r2019b release. If you find yourself dealing with that problem, here's a solution.

Accedi per commentare.

Più risposte (1)

Rik
Rik il 4 Ott 2019
Did you notice this line?
% Begin initialization code - DO NOT EDIT
They weren't kidding. It is easy to mess up GUIDE-created GUIs. I'm not seeing the closing of this comment, so something happened to that code. Somehow the code responsible to making the GUI visible isn't running. The code below should be a temporary fix to make your GUI visible again.
h=GUI_start;
set(h,'Visible','on')

2 Commenti

Yes I noticed this line. And
% End initialization code - DO NOT EDIT
is the only thing that is missing there. I cecked it by creating a new GUI. I really didn't change anything there.
Okay Thank you. This is working :-)
Visibility was indeed the issue but the user would have to run this line of code every time they access the GUI. Even if this line below were placed in the opening function, the GUI will still not stay visible as long as the visibility parameter in the figure file is set to 'off' which can only be changed through GUIDE.
handles.figure1.Visible = 'on';

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by