Executing Arduino commands from within a function in MATLAB

I have a very basic question, so basic it seems that two days of internet searches and a thorough look through these and MATLAB's forums did not yield a solution. I'm attempting to write a function in matlab, in which I would like to use simple commands like digitalWrite. However when I try to run the function it appears that the variable representing the arduino interface is not recognized. I'm sure there is some simple explanation for this but I can't seem to find it. Here is a simple program I wrote that sends some values into a shift register and then enables them in order to light 8 leds. It works when run as a script, but I'm curious why it wont work in a function. If anyone could help me out, it would be greatly appreciated.

This is my error:
The class "a" is undefined.
Perhaps Java is not running.

BTW these commands are entered in beforehand:

a=arduino('COM5');

a.pinMode(12,'output');
a.pinMode(2,'output');
a.pinMode(4,'output');
a.pinMode(6,'output');
a.pinMode(8,'output');

function [ ] = shiftreg( Serial )

    function [] = clkpulse()
        digitalWrite(a,clk,1);  % Clock Pulse
        digitalWrite(a,clk,0);
    end
function [] = enclkpulse()
        digitalWrite(a,clk,1);  % Clock Pulse
        digitalWrite(a,clk,0);
    end
S=Serial;    % Column 

ser = 12;
en = 2;
clk = 4;
enclk = 6;
clr = 8;
% Setup
digitalWrite(a,clr,0);  % Clear Shift Register
digitalWrite(a,clr,1);  % Turn Clear Off (High)
digitalWrite(a,en,1);   % Enable Off (High)
digitalWrite(a,enclk,0);  % enclk set to 0

% Input Serial Data
n=1;
while n<9
digitalWrite(a,ser,S(n,1));  
clkpulse()
n = n+1;
end

enclkpulse() 

digitalWrite(a,en,0);  % Enable

end

Why do you think that that should work? Why do you think that arduino() is defined? Why do you think that asking Matlab question on the Arduino forum is appropriate? Do you ask your Arduino questions on the Matlab forum?

Why do you think that that should work?

Because I have seen other programs that use the arduino variable/object (not sure which) in functions in various threads.

Why do you think that arduino() is defined?

Not sure of the answer to this question as I'm just starting out. I was hoping others might be able to help.

Why do you think that asking Matlab question on the Arduino forum is appropriate?

Because Is deals with interfacing the arduino with software on my computer (MATLAB) as is stated in the title of this forum.

Do you ask your Arduino questions on the Matlab forum?

I plan on it in this case. I was just hoping someone here might know the answer.

Why are you replying to my thread when you have nothing positive or helpful to contribute?

Charlie5050:
Because I have seen other programs that use the arduino variable/object (not sure which) in functions in various threads.

Have you a link to some of those Threads? Are you doing it the same way? If not, why not?

Because Is deals with interfacing the arduino with software on my computer (MATLAB) as is stated in the title of this forum.

That may be true, but it still requires a knowledge of Matlab rather than a knowledge of Arduino stuff.

Why are you replying to my thread when you have nothing positive or helpful to contribute?

Questions which cause the reader to think a bit more about a problem can be more useful in the long run than answers that eliminate the need for thought.

...R