If you have the Arduino support package, you can directly read the values of the digital I/O pins. After that, it's a matter of logic to determine the press count: a = arduino;
button_pin = 'D13';
pin_value_save = 0;
count = 0;
while true
pin_value = readDigitalPin(a, button_pin);
if pin_value ~= pin_value_save
pin_value_save = pin_value;
count = count + 1;
end
delay(50/1000);
end
If you need this to run in the background while your program does other things it gets significantly more complex, though. MATLAB only has interrupts in Simulink and GUI callback functions, so you'll need to restructure things in that way.
If you don't have the support package, you'll want to have Arduino send a signal when the value of the button pin changes, using the Serial.println() function in Arduino and the serial functions in MATLAB to interpret the results. The best part of this method is that the Arduino's serial buffer will hold any values (up to 64) from the Arduino until the script gets back to it, so you can ask MATLAB to do other things and just read in the buffer when possible. Then, you can just count the size of the buffer to figure out how many more events have happened since the last reading.