How to load data with using detectimportsoption
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a large text file and headers is repeating in the file. How should i write a new file with only one header row instead of multple with delimiter ','
here is the file looks like;
'TRACK\LINK12\162\A\B\\\\\\\\'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'TRACK\LINK12\162\A\B\\\\\\\\'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'TRACK\LINK12\162\A\B\\\\\\\\'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
infile = 'C;\example\example.wam'
tmp = detectImportOptions(infile, 'FileType', 'text')
tmp.Delimiter = ','
t = readtable(infile, tmp)
writetable(t, 'Newfile.csv', 'delimiter', ',')
0 comentarios
Respuestas (2)
Image Analyst
el 28 de Oct. de 2020
You forgot to attach example.wam so I made up one according to what you said.
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
fullInputFileName = fullfile(pwd, 'example.wam');
if ~isfile(fullInputFileName)
errorMessage = sprintf('Error: file not found:\n%s', fullInputFileName);
uiwait(errordlg(errorMessage));
return;
end
tmp = detectImportOptions(fullInputFileName, 'FileType', 'text')
tmp.Delimiter = ','
t = readtable(fullInputFileName, tmp)
% Create output file.
fullOutputFileName = fullfile(pwd, 'Newfile.csv');
fid = fopen(fullOutputFileName, 'wt')
wroteHeaderRowYet = false;
for k = 1 : size(t, 1)
% Skip rows that contain 'TRACK\LINK12\162\A\B\\\\\\\\'
% unless it's the first one.
thisLine = char(t{k, 1});
if contains(thisLine, 'TRACK\LINK12\162\A\B\\\\\\\\')
if ~wroteHeaderRowYet
fprintf(fid, '%s\n', thisLine);
wroteHeaderRowYet = true;
end
else
fprintf(fid, '%s\n', thisLine);
end
end
fclose(fid);
% writetable(t, 'Newfile.csv', 'delimiter', ',')
fprintf('\nDone running %s.m ...\n', mfilename);
type(fullOutputFileName); % Let's see what we ended up with by typing it to the command window.
If it doesn't work, then attach the REAL example.wam file, but you have to zip it up first.
3 comentarios
Ver también
Categorías
Más información sobre Environment and Settings 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!