user29533: I'm trying to use Arduino in Matlab. cause I used Arduino in some function I had to define Arduino as a global variable. 
clc,clear,close all;
global a
a = arduino();
.
some functions
.
clear a

at the end of my code I clear the variable but as I run the code again this error appear:
Failed to open serial port (user COM) to communicate with board Uno. ...
 Make sure there is no other MATLAB arduino object for this board.

I guess the global function can't be clear by clear command so I tried clear all instead clear in the last line. It works perfect as I run it again and again but as you know this is Matlab tips for clear all command
Using clear all causes MATLAB® to recompile previously cached code, 
and can decrease performance.

I've tried delete(a) so the result was: 
Error using arduino/delete
Cannot access method 'delete' in class 'arduino'.

any help would be appreciated.

user18916: In the MatLab Help Center they had nice description of how to clear a variable from memory. Check it here: https://www.mathworks.com/help/matlab/ref/clearvars.html 
In your case should be: clearvars -global a;

29533: <p>I'm trying to use <code>Arduino</code> in <code>Matlab</code>. cause I used Arduino in some function I had to define <code>Arduino</code> as a global variable. </p>

<pre><code>clc,clear,close all;
global a
a = arduino();
.
some functions
.
clear a
</code></pre>

<p>at the end of my code I <code>clear</code> the variable but as I run the code again this error appear:</p>

<pre><code>Failed to open serial port (user COM) to communicate with board Uno. ...
 Make sure there is no other MATLAB arduino object for this board.
</code></pre>

<p>I guess the global function can't be clear by <code>clear</code> command so I tried <code>clear all</code> instead <code>clear</code> in the last line. It works perfect as I run it again and again but as you know this is <code>Matlab</code> tips for <code>clear all</code> command</p>

<pre><code>Using clear all causes MATLAB® to recompile previously cached code, 
and can decrease performance.
</code></pre>

<p>I've tried <code>delete(a)</code> so the result was: </p>

<pre><code>Error using arduino/delete
Cannot access method 'delete' in class 'arduino'.
</code></pre>

<p>any help would be appreciated.</p>
 18916: <p>In the MatLab Help Center they had nice description of how to clear a variable from memory. Check it here: <a href="https://www.mathworks.com/help/matlab/ref/clearvars.html" rel="nofollow noreferrer">https://www.mathworks.com/help/matlab/ref/clearvars.html</a> </p>

<p>In your case should be: <code>clearvars -global a;</code></p>
