Separating and sorting a character string of magnetometer data (X,Y,Z,temperature) into separate arrays of doubles
Show older comments
I am working on a project where I am taking I2C data from a magnetometer>arduino>matlab. The data comes in as a character string, example: X-123.3 Y30.3 Z500.4 t24.5, randomly pulls data so the string starts at a random reading or even part way through a value.
I am having issues getting the code to run consistently without throwing the error "Conversion to double from cell is not possible." when I try to sort the separated data into individual arrays for each axis and temperature. I have attached the .m file and copied the code below. Any assistance would be greatly appreciated.
s1 = serialport('/dev/tty.usbserial-1440', 9600);
fopen('s1');
n = 300;
j=linspace(1,n);
k=1:25;
for i=1:length(j)
data = read(s1,n,"char");
data_split = strsplit(data);
end
for m=1:length(k)
data_tem = data_split(m);
data_t = char(data_tem);
if data_t(1) == 'x'
cx(m,:) = data_tem; %%ERROR THROWN HERE
elseif data_t(1) == 'y'
cy(m,:) = data_tem;
elseif data_t(1) == 'z'
cz(m,:) = data_tem;
elseif data_t(1) == 't'
temp(m,:) = data_tem;
end
end
cx = cx(~cellfun('isempty', cx));
cx = char(cx);
cx(:,1) = [];
cx = str2num(cx)
%magX = abs(magX);
calx = average(cx)
cy = cy(~cellfun('isempty', cy));
cy = char(cy);
cy(:,1) = [];
cy = str2num(cy)
%magY = abs(magY);
caly = average(cy)
cz = cz(~cellfun('isempty', cz));
cz = char(cz);
cz(:,1) = [];
cz = str2num(cz);
cz = abs(cz)
calz = average(cz)
clear s1
If more information is needed please let me know and I will happily provide.
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!