How can I parse this textfile with textscan? Delimiter \t not working
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Munho Noh
el 27 de Jul. de 2023
Comentada: Munho Noh
el 27 de Jul. de 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1443177/image.jpeg)
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?
0 comentarios
Respuesta aceptada
Stephen23
el 27 de Jul. de 2023
Editada: Stephen23
el 27 de Jul. de 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)
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!