Effacer les filtres
Effacer les filtres

How can I parse this textfile with textscan? Delimiter \t not working

3 vues (au cours des 30 derniers jours)
Munho Noh
Munho Noh le 27 Juil 2023
Commenté : Munho Noh le 27 Juil 2023
I'd like to parse this static_vehicle_characteristics_svc.svc file into three groups of blocks (General, Front Sus, Rear Sus) and split the group of blocks into 5 headers (Parameter, Units, Average, Left, Right) and store data into each header.
Here's my code, I was trying to make this as a function. (input : variable name, suspension type / output : data of input variable)
If I put input as function('General Characteristcs', 'Total weight'), I want this function to return all three values of total, left, right stored in structure or something like that so I could access to use the values in other script. I will make this into a function on my own if someone helps me parsing data.
clc
clear all
file_name = 'Static_Vehicle_Characteristics_svc.txt'; %
str = fileread(file_name);
% Regular Expression to split strings into general, fr, rr character blocks
xpr = '[A-Z].+?(CHARACTERISTICS).+?(\r\n){3}';
blocks = regexp(str, xpr, 'match');
num_blocks = length(blocks);
% For each block I'd like to parse text into variable name, units, total,
% left, right
for i = (2:num_blocks)
block(i-1).data = textscan(blocks{i}, '%s%s%s%s%s', 'Delimiter', '\t', 'HeaderLine', 6);
% block(i-1).data = regexp(blocks{i}, '[A-Z].+?\r\n', 'Match'); %breaks into lines
% block(i-1).data(cellfun(@isempty, block(i-1).data)) = []; %remove empty lines
% block(i-1).each_line = textscan([block(i-1).data{:}], '%s', 'Delimiter', ' ');
% block(i-1).each_line(cellfun(@isempty, block(i-1).each_line)) = [];
end
At first I thought, each block would be split into cell array of 5 columns if I use delimiter \t. But the result was not what I expected.
The output I got just splits each block into lines.
I tried to split each line with whitespaces, but it returns so many lines and doesn't look smart.
What did I do wrong and how can I fix this?

Réponse acceptée

Stephen23
Stephen23 le 27 Juil 2023
Modifié(e) : Stephen23 le 27 Juil 2023
"At first I thought, each block would be split into cell array of 5 columns if I use delimiter \t. But the result was not what I expected."
That text file does not contain one single tab character, so it is unclear to me how you thought that specifying a non-existent delimiter character would help to import that file. The file is basically fixed-width, not delimited....
But unfortunately that file is not quite a fixed-width file (e.g. see lines 33, 44), but we might be able to define a broad enough specification anyway, that can import those numeric values correctly:
fnm = 'Static_Vehicle_Characteristics_svc.txt';
opt = detectImportOptions(fnm, 'FileType','fixedwidth', 'ReadVariableNames',false,...
'Range',22, 'VariableWidths',[34,11,11,11,99]);
opt.VariableNames = {'PARAMETER','UNITS','TOTAL','LEFT','RIGHT'};
tbl = readtable(fnm, opt)
tbl = 158×5 table
PARAMETER UNITS TOTAL LEFT RIGHT _________________________ ____________ _________ ____ _____ {'Total weight' } {'N' } 10000 NaN NaN {'Front ground reaction'} {'N' } 1000 1000 1000 {'Rear ground reaction'} {'N' } 1000 1000 1000 {'Total roll inertia' } {'kg mm**2'} 1.006e+08 NaN NaN {'Total pitch inertia' } {'kg mm**2'} 1e+09 NaN NaN {'Total yaw inertia' } {'kg mm**2'} 1e+09 NaN NaN {'Total product Ixy' } {'kg mm**2'} 2000.4 NaN NaN {'Total product Ixz' } {'kg mm**2'} 1e+06 NaN NaN {'Total product Iyz' } {'kg mm**2'} -1000 NaN NaN {'Sprung mass' } {'kg' } 1000 NaN NaN {'Sprung roll inertia' } {'kg mm**2'} 1.004e+08 NaN NaN {'Sprung pitch inertia' } {'kg mm**2'} 1e+09 NaN NaN {'Sprung yaw inertia' } {'kg mm**2'} 1e+09 NaN NaN {'Sprung product Ixy' } {'kg mm**2'} 1000 NaN NaN {'Sprung product Ixz' } {'kg mm**2'} -1e+06 NaN NaN {'Sprung product Iyz' } {'kg mm**2'} -1000 NaN NaN
Obviously you need to check that all values are imported as expected, adjust the import settings (e.g. variable widths) as required. After that you will then need some post-processing to remove some of the invalid data lines, identify the blocks, etc.
  1 commentaire
Munho Noh
Munho Noh le 27 Juil 2023
Thanks a lot. It just looked like it had a delimiter. I will try this out.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by