MATLAB: How to Split numbers into matrix

arduinosplit

Hi guys, im currently working on EMG recording trough Arduino and EMG Shield. the problem is that i have to record the signal from two muscles, since that, i have to read two values from to arduino and save into a Matlab variable the kind of values that comes from arduino are two numbers like 514;125 so, how can i read those values and save them into a matrix with two columns?? this is my code from recording so far.
if true
% code
end
delete(instrfind({'Port'},{'COM3'}));
port=serial('COM3');
port.BaudRate=115200; %
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
fopen(port);
Fs=1/5000;
t=0;
i=1;
tic;
tmax=2;
while t<tmax;
if toc >(Fs)
valor=fscanf(port,'%d') %here i read the output from arduino
raw(i)=valor(1,1);
t=t+toc;
tic;
end
fclose(port);
delete(port);
thx

Best Answer

You should update the index i inside your while, if loop. Also you now only store the first value of valor.
raw = zeros(1000,2) ; % pre-allocation of a buffer for speed
i = 0 ;
...
while t < tmax
valor = ...
i = i + 1 ; % put it in the next row
raw(i,1:numel(valor)) = valor ; % will work if valor has less or more than 2 elements
...
end
raw = raw(1:i,:) ; % truncate unfilled rows
Related Question