Arduino doesn't respond to serial input from MATLAB

Hello all,
I was working on a project wherin it is required to send inputs from matlab to Arduino serially through usb. The Arduino accepts the data but it is not responding.
I have tried changing the baud rates but doesn’t help. But when i give inputs from serial monitor the Arduino responds perfectly. Please help

Please post your Arduino and MATLAB code. We can't be much help without that.

What exactly is your project supposed to do (as detailed as possible).

here is the code sir
please do help

sketch_may21a.ino (2.99 KB)

MY project is about reading brain signals through eeg headdset into matlab. And send alphabets A B C D from matlab to arduino through serial port based on the value of the brain signals.
the problem is the data is sent from matlab to arduino but arduino doesnt operate as per the program.
when giving input through matlab command window it does the desired operation.

matlab Code

TG_DATA_ATTENTION = 2;

%$Matlab code to read attention using Mindwave mobile

%Clear Screen

clc;

%Clear Variables

clear all;

%Close figures

close all;

%Preallocate buffer

data_att = zeros(1,256);

%Comport Selection

portnum1 = 14;

%COM Port #
s = serial('COM10','Baudrate',9600);

comPortName1 = sprintf('\\\\.\\COM%d', portnum1);

% Baud rate for use with TG_Connect() and TG_SetBaudrate().

TG_BAUD_115200 = 9600;

% Data format for use with TG_Connect() and TG_SetDataFormat().

TG_STREAM_PACKETS = 0;

% Data type that can be requested from TG_GetValue().

TG_DATA_ATTENTION = 2;

%load thinkgear dll

loadlibrary('Thinkgear.dll');

%To display in Command Window

fprintf('Thinkgear.dll loaded\n');

%get dll version

dllVersion = calllib('Thinkgear', 'TG_GetDriverVersion');

%To display in command window

fprintf('ThinkGear DLL version: %d\n', dllVersion );

% Get a connection ID handle to ThinkGear

connectionId1 = calllib('Thinkgear', 'TG_GetNewConnectionId');

if ( connectionId1 < 0 )

error( sprintf( 'ERROR: TG_GetNewConnectionId() returned %d.\n', connectionId1 ) );

end;

% Attempt to connect the connection ID handle to serial port "COM3"

errCode = calllib('Thinkgear', 'TG_Connect', connectionId1,comPortName1,TG_BAUD_115200,TG_STREAM_PACKETS );

if ( errCode < 0 )

error( sprintf( 'ERROR: TG_Connect() returned %d.\n', errCode ) );

end

fprintf( 'Connected. Reading Packets...\n' );

i=0;

j=0;

%To display in Command Window

disp('Reading Brainwaves');

figure;

while 1

if (calllib('Thinkgear','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...

if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ATTENTION ) ~= 0)

j = j + 1;

i = i + 1;

%Read attention Valus from thinkgear packets

data_att(j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION );


%To display in Command Window

disp(data_att(j));
fopen(s);

if(data_att(j)>5 && data_att(j)<35)
% disp('Forward');
% fopen(s);
        fprintf(s,'%s','A');
        %fprintf(s,'A');
        % pause(1);
       %  resume();
%         fclose(s);
end
if(data_att(j)>35 && data_att(j)<65)
% disp('right');
% fopen(s);
        fprintf(s,'%s','B');
       % fprintf(s,'B');
        %pause(1);
      %  resume();
%         fclose(s);
end
if(data_att(j)>65 && data_att(j)<95)
%disp('left');
% fopen(s);
        fprintf(s,'%s','C');
        
         %pause(1);
       %  resume();
      %  fclose(s);
end
if(data_att(j)==0)
%disp('robot arm');
% tic;
%while toc < 10
 %   end
% fopen(s);
        fprintf(s,'%s','D');
         %pause(1);
     %    resume();
%        fclose(s);
end
%Plot Graph
fclose(s);
plot(data_att);

title('Attention');

%Delay to display graph

%pause(1);

end

end

end

%To display in Command Window

disp('Loop Completed')

%Release the comm port

calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );

Matlab is expensive. Most people won’t have it on their computers to help you debug.
You will have to learn how to use your brain and troubleshoot this.

I have been trying to troubleshoot this but in vain.
I thought there might be someone who'll help me out

May be you will luck out. I don't have Matlab.
If you don't learn now how to think through a problem and learn to solve it, later while on the job won't be a good time.

ok sir

yeshas436:
here is the code sir
please do help

You have no code in the Arduino program to let you see what it is actually receiving.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R