Separate 24 digits single array of data loaded from file into 6 different arrays
1 view (last 30 days)
Show older comments
Luis Felipe Brunhara
on 1 Sep 2021
Commented: Luis Felipe Brunhara
on 1 Sep 2021
So I have a datalogger saving into a file data from 6 diferent sensors with this format:
504800002206203200000000
504800002205203000000000
504800002205203000000000
507000002206202900000000
507000002206203000000000
504200002205203200000000
504200002206203100000000
504200002205203200000000
504200002208203100000000
504200002204203200000000
500400002205203000000000
Where Each 4 Digits is equivalent to a singe data, for a total of 24 digits (6*4) per line.
I want to split this into 6 variables (one for each sensor data).
I came up with a code that does that, but it is too slow. Does anybody have an suggestion on how to improve it?
%===|Converte em String|===
d = textscan(fid,'%s');
fclose(fid);
dados = d{1,1};
%===|Converte a String em Dados|===
for i=1:length(dados)
dados{i,1} = split(dados{i,1},"");
dados{i,2} = str2double(sprintf('%s%s%s%s',dados{i,1}{6,1}, dados{i,1}{7,1}, dados{i,1}{8,1}, dados{i,1}{9,1}));
dados{i,3} = str2double(sprintf('%s%s%s%s',dados{i,1}{10,1}, dados{i,1}{11,1}, dados{i,1}{12,1}, dados{i,1}{13,1}));
dados{i,4} = str2double(sprintf('%s%s%s%s',dados{i,1}{14,1}, dados{i,1}{15,1}, dados{i,1}{16,1}, dados{i,1}{17,1}));
dados{i,5} = str2double(sprintf('%s%s%s%s',dados{i,1}{18,1}, dados{i,1}{19,1}, dados{i,1}{20,1}, dados{i,1}{21,1}));
dados{i,6} = str2double(sprintf('%s%s%s%s',dados{i,1}{22,1}, dados{i,1}{23,1}, dados{i,1}{24,1}, dados{i,1}{25,1}));
dados{i,1} = str2double(sprintf('%s%s%s%s',dados{i,1}{2,1}, dados{i,1}{3,1}, dados{i,1}{4,1}, dados{i,1}{5,1}));
end
dados = cell2mat(dados);
%===|Salva os Dados em Variaveis|===
rot = dados(:,1);
vel = dados(:,2);
anlg0 = dados(:,3);
anlg1 = dados(:,4);
tempAmb = dados(:,5);
tempObj = dados(:,6);
0 Comments
Accepted Answer
Simon Chan
on 1 Sep 2021
Try the following by converting the text using function num2cell.
clear; clc;
fid = fopen('data.txt');
d = textscan(fid,'%s');
data = cat(1,d{:});
dataexpand = cellfun(@num2cell,data,'UniformOutput',false);
alldata = cat(1,dataexpand{:});
%
rot = str2double(string(cell2mat(alldata(:,1:4))))
vel = str2double(string(cell2mat(alldata(:,5:8))))
anlg0 = str2double(string(cell2mat(alldata(:,9:12))))
anlg1 = str2double(string(cell2mat(alldata(:,13:16))))
tempAmb = str2double(string(cell2mat(alldata(:,17:20))))
tempObj = str2double(string(cell2mat(alldata(:,21:24))))
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!