Import not consistent table data

Hi! Sorry again for a (maybe) naif question, but I have been struggling all day trying to figure out how I can import a not consistent .txt dataset. I show you in figure what I mean:
From that, I just need to extract the column with inside the blu limiters. I tried to set import option and get the column where those numbers are in by the following code:
opts = detectImportOptions(sample_txt_file);
disp([opts.VariableNames' opts.VariableTypes']);
opts = setvartype(opts,{'x1100071'},'char');
disp([opts.VariableNames' opts.VariableTypes']);
values = readtable(sample_txt_file,opts);
times = values{:,4};
As result, I got 4 table columns and then I extracted the last one, the one with numbers I need.
Checking on how I could change 'char' values into a numeric array I coded:
S = sprintf('%s',times{:});
D = sscanf(S, '%f');
So I got an array containing number values of all those from column 4 but still don't understand how it works or if there are more suitable ways to extract data from that dataset.
Hope you could help me and thank you again :)

3 Commenti

hello
can you provide your txt file ?
I attach here the sample.zip. Thank you :)
do you want separate blocks of data or everything in one block ?

Accedi per commentare.

 Risposta accettata

hello again
so this first simple code will extract the data as one single block
if you need to separate the data in individual blocks as organized in your file , that needs a bit of extra code but shoudn't be very difficult :
str = readlines('samples.txt');% read the file:
C = regexp(str,' MODfromPPD:: PARA:'); % find lines that contains that string
ind = find(~cellfun('isempty',C)); % indice of non empty cells
data_lines = split(str(ind)); % split columns of selected rows
your_data = str2double(data_lines(:,7)); % your data are in the 7th column of data_lines

3 Commenti

in the mean time , this is the code extended that puts data in blocks like in the txt file
one block of data is stored in a cell - you have now a cell array of dimensions 4000 x 1 containing your 4000 blocks of data
hope it helps
str = readlines('samples.txt');% read the file:
C = regexp(str,' MODfromPPD:: PARA:'); % find lines that contains that string
ind = find(~cellfun('isempty',C)); % indice of non empty cells
data_lines = split(str(ind)); % split columns of selected rows
your_data = str2double(data_lines(:,7));
% split in blocks
ind_blocks = str2double(data_lines(:,6)); %
ind_first_line = find(ind_blocks<2); % find start lines correponding to value = 1
for ck = 1:numel(ind_first_line)-1
block{ck} = your_data(ind_first_line(ck):ind_first_line(ck+1)-1,1);
end
% add the last block
block{ck+1} = your_data(ind_first_line(end):end,1);
Yes, it worked! I just needed not splitted so the first code was good, but I thank you for you detailed and helpful answer. It was so kind of you.
I think I should spend more time on how to import different datatypes but your explanation helped me much! :)
yes , it takes some time to figure out how to deal with the multiple file formats , but after some time you go faster as you get more experienced.
also this forum is a good place to ask and learn from other's Q & A

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by