classdef GuiScanner_1 < matlab.apps.AppBase
properties (Access = public)
UIFigure matlab.ui.Figure
GUITESTLabel matlab.ui.control.Label
READER1Button matlab.ui.control.StateButton
READER2Button matlab.ui.control.StateButton
end
delete(instrfindall);
clear
clc
The line of code where you delete the objects returned by instrfindall is not inside any method of the class GuiScanner_1. Rather than copying your code into the class definition file, I would create a button or something else that when activated you want to run your code, and write a callback for that component in your app to call the script you've written. Why make a copy of the script in your app, which could get out of sync with your original script file as you fix bugs or add new features to one but not the other?
By the way, I would eliminate these lines from your script.
delete(instrfindall);
clear
clc
clear eliminates all the variables in the workspace in which it's called. If this script gets called from a component's callback function that's more limited in scope, but you're still going to eliminate some of the useful variables the app passes into the callback by default. Usually it should be run interactively, at the MATLAB Command Prompt, when you've made a conscious decision that you want to clear the workspace.
clc is another tool with wide ranging effects. Again, I recommend using this interactively rather than in a script or function. Using it in an app (which a user likely would not expect to interact at all with the Command Window) is likely to lead to at least mild user confusion or annoyance.
delete(instrfindall) suggests to me that someone created an instrument object and forgot or chose not to delete that instrument object once they were finished. As this documentation page states "A good programming practice is to make sure that you leave your program environment in a clean state that does not interfere with any other program code." If what you're cleaning up is the object you created with:
I would create an onCleanup to close and delete the serial object when the callback function that runs your script finishes its execution.
Best Answer