MATLAB-Arduino communication keeps breaking

Hello,

I am trying to drive a bipolar stepper motor using MATLAB. Instead of Arduino's stepper library, I use MATLAB itself to give the digitalWrite commands to 2 output pins in a pattern such that the stepper motor will spin. An H bridge in my circuit takes care of inverting/splitting the 2 signals into the 4 that the bipolar stepper motor needs to operate.

The motor moves as told until after about 10 commands are given or after about a minute passes. After that, the communication is broken and I cannot give any commands to the Arduino. Shortly after, a strange whining noise begins to come out of the motor. Can someone please help me figure out why this is happening and how to maintain the communcation?

Code & error messages below:

MATLAB Code

%----Initialization code---------
global a;
a = arduino('COM4');
a.pinMode(8,'output');
a.pinMode(9,'output');

%-----Executes on button press--------------
%Retrieve speed and step information from GUI textboxes
speed = str2double(get(handles.edit_speed,'String'));               %rev/s
steps = str2double(get(handles.edit_steps,'String'));               %steps

%Calculate delay between signals.
stepsPerRev = 200;   %steps/rev
stepDelay = (1/stepsPerRev)*(1/speed);                              %s/step


%Step the motor 
for k = 1:steps
        a.digitalWrite(8,1);
      a.digitalWrite(9,0);
      pause(stepDelay);
      a.digitalWrite(8,1);
      a.digitalWrite(9,1);
      pause(stepDelay);
         a.digitalWrite(8,0);
      a.digitalWrite(9,1);
      pause(stepDelay);
      a.digitalWrite(8,0);
      a.digitalWrite(9,0);
      pause(stepDelay);
end

Error Messages

Error using serial/fwrite (line 199)
Unscussessful write: An error occurred during writing.

Error in arduino/digitalWrite (line 563)
                fwrite(a.aser,[50 97+pin 48+val],'uchar');

Error in stepper01>button_left_Callback (line 204)
      a.digitalWrite(8,1);

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in stepper01 (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)stepper01('button_left_Callback',hObject,eventdata,guidata(hObject))

 
Error while evaluating uicontrol Callback

Can someone please help me figure out why this is happening and how to maintain the communcation?

Without seeing the Arduino code? Not likely.

PaulS:

Can someone please help me figure out why this is happening and how to maintain the communcation?

Without seeing the Arduino code? Not likely.

I run adioes.pde that comes with the MATLAB IO package; runs in the background

Those error messages are MATLAB errors as I do not recognize them as Arduino errors.
Also the line numbers mentioned in the errors do not match the number of lines of code you have posted so it is quite difficult to say something meaningful without access to the whole MATLAB code.

The part you posted seems to be quite straightforward (no expert on Matlab)
and I do not see things that triggers my "bug nerves"

So please post your whole Matlab sketch (or make a minimal sketch that shows the problem!)