This has been tremendously difficult for something I thought would be very straightforward. I've had absolutely no luck thus far.
SInce you are doing:
fprintf(serialPort,'%d',data(i)); %write the data
You are running your numbers all together. The output of the matlab code if you printed a 25 followed by a 123 would be 25123 and the arduino wouldn't know where the beginning/end of the numbers were.
I suggest (again, untestested)
fprintf(serialPort,'%d\n',data(i)); %write the data
I know nothing about Matlab string formats so it might use " instead of ' for special character expansion.
I'd also use the led blink I mentioned if all else fails to see if you are getting anything at all.
Unfortunately, while I think you're close, this code hasn't worked for me. I tried tweaking it myself many times as well to no avail. Each time I try to run it, and open the serial monitor, I don't see anything at all being printed. I beginning to wonder if the data from MATLAB is even reaching the serial monitor in the first place.
As for the LED blink, I found the following code online, and it seems to work perfectly:
answer=1; % this is where we'll store the user's answer
arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
fopen(arduino); % initiate arduino communication
fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
fclose(arduino); % end communication with arduino
if(Serial.available()>0) // if there is data to read
matlabData=Serial.read(); // read data
digitalWrite(ledPin,HIGH); // turn light on
digitalWrite(ledPin,LOW); // turn light off
When I tried running this it worked fine-I was able to turn the LED on and off by entering numbers in MATLAB. Perhaps the biggest difference with what's happening here and what I want to do is have both programs "use" the serial monitor? I'm not sure if that's even the case or what exactly it is I'm doing wrong at this point.
You may find the examples in serial input basics useful for a simple reliable way to receive data. There is also a parse example
You need to design the system for sending data at the same time as the system for receiving it. Generally it is easier to tailor PC code to suit an Arduino than vice versa.
Aside from our fellows orientations, you might want to take a look at the code I just posted in this page in order to help another fellow user.
Robin, I think the bolded is key-I guess I'm just not capable of doing something like that at this point. Do you have specific suggestions?
However, thank you both for posting this. Although I wasn't able to find anything in these two links that helped me solve my issue, they were useful in that they gave me a better basic understanding of serial communication.
If I can't transfer data from MATLAB directly into Arduino, does anyone have any other suggestions? Is it possible for Arduino to simply read from a text file, for example? Although I'd much prefer to use MATLAB, I think I just need to solve this problem anyway I can.