How to load data with using detectimportsoption

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', ',')

답변 (2개)

Image Analyst
Image Analyst 2020년 10월 28일

0 개 추천

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

Thanks, I will check and let you know. I appreciate your help
I checked your code and it does almost what i want to do but in Newfile.csv it does not sepearte the each elements in different col. currently whole string printing in col 1, but i want to sepearte each word into different col.
'POINT\2020\003\124\835\4000\M\0.78\W\0.0000\0.0000......'
col1 col2 col3 col4 col5 ........
Point 2020 003 124 835 ......
Also, how should i write a data in sepearte .csv file whenever it is occuring a different header line.
Thanks for your help. I appreciate it.
Did you overlook the last sentence of my last comment?

댓글을 달려면 로그인하십시오.

Utsav Dobhi
Utsav Dobhi 2020년 10월 29일

0 개 추천

Hi,
I can not attached the .wam extension file down here. i got an error. Therefore, i transfered the .wam into .txt file.

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

질문:

2020년 10월 28일

답변:

2020년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by