App designer - close app - Invalid or deleted object.

6 visualizaciones (últimos 30 días)
Dominik Pavlica
Dominik Pavlica el 7 de Mayo de 2022
Respondida: Hari el 13 de Nov. de 2024 a las 8:15
Hello,
I would like to ask if anyone would know how to get rid of the error message if the for loop is running and I close the application.
number_of_values = length(simulace.PID_regulator_regulacni_velicina{1}.Values.Data);
hold(app.Graph,'on')
plot(app.Graph,simulace.PID_regulator_regulacni_velicina{1}.Values.Time(1:pocet_hodnot),simulace.PID_regulator_regulacni_velicina{1}.Values.Data(1:pocet_hodnot),'b--', 'LineWidth', 1);
graf_regulovana_velicina = plot(app.Graph,simulace.PID_regulator_regulacni_velicina{2}.Values.Time(1),simulace.PID_regulator_regulacni_velicina{2}.Values.Data(1), 'LineWidth', 2);
hold(app.Graph,'off')
for r = 1:pocet_hodnot
hold(app.Graph,'on')
set(graf_regulovana_velicina,'Ydata',simulace.PID_regulator_regulacni_velicina{2}.Values.Data(1:r),'Xdata',simulace.PID_regulator_regulacni_velicina{2}.Values.Time(1:r));
hold(app.Graph,'off')
hold(app.Animation, 'on')
Y = [11-(y/2)+((simulace.PID_regulator_regulacni_velicina{2}.Values.Data(r))*pomer_vykreslene_vysky_nadrze_k_zadani_vysce_zasobniku), 11-(y/2), 11-(y/2), 11-(y/2)+((simulace.PID_regulator_regulacni_velicina{2}.Values.Data(r))*pomer_vykreslene_vysky_nadrze_k_zadani_vysce_zasobniku)];
set(tank,'Ydata', Y);
pause(time)
fill(app.Animation, t1, t2, [0.85 0.85 0.85], 'LineStyle','none');
fill(app.Animation, t3, t4, [0.85 0.85 0.85], 'LineStyle','none');
pause(0.002)
fill(app.Animation, t1, t2, 'b', 'LineStyle','none');
fill(app.Animation t3, t4, 'b', 'LineStyle','none');
hold(app.Animation, 'off')
end
This is only part of the code, but in this part of the code I get an error if it is running in a loop and I close the application. He's probably trying to approach an object that no longer exists in that for loop, but I'm not sure how to solve this problem. I tried to solve the problem via closeFcn, but in vain.
Error message:
Invalid or deleted object.
Matlab version: 2020a
Thank you for any help

Respuestas (1)

Hari
Hari el 13 de Nov. de 2024 a las 8:15
Hi Dominik,
I understand that you are encountering an error message when closing your application while a "for" loop is running. The error likely arises because the loop attempts to access an object that no longer exists after the application is closed.
I assume in the MATLAB App Designer application you are using, you have access to modify the event handlers and loop logic. Here are some troubleshooting steps you can try:
  1. Error Handling with Try-Catch: You can wrap the loop with a "try-catch" block to gracefully handle any errors that occur if the application is closed during execution.
  2. Check for Valid Object Handles: Before updating graphics objects, ensure that they are valid. This can prevent errors when the application is closed.
  3. Use an App Property to Control Loop Execution: Create a property in your app that indicates whether the loop should continue running. Set this property to "false" when the application is closing.
  4. Implement the Close Request Function: In the "CloseRequestFcn" of your app, set the property to "false" to stop the loop when the app is closing.
  5. Update the Loop Condition: Modify the loop to check the "ContinueLoop" property to ensure it stops when the application is closing.
Refer to the documentation of the "try-catch" statement for more information: https://www.mathworks.com/help/matlab/ref/try.html
Refer to the documentation on custom close request functions: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#d123e321341
Hope this helps!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by