MATLAB Answers

How to load data with using detectimportsoption

17 views (last 30 days)
Utsav Dobhi
Utsav Dobhi on 28 Oct 2020 at 18:27
Answered: Utsav Dobhi on 29 Oct 2020 at 14:00
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 Comments

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 28 Oct 2020 at 19:39
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 Comments

Utsav Dobhi
Utsav Dobhi on 28 Oct 2020 at 19:46
Thanks, I will check and let you know. I appreciate your help
Utsav Dobhi
Utsav Dobhi on 29 Oct 2020 at 13:18
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.
Image Analyst
Image Analyst on 29 Oct 2020 at 13:41
Did you overlook the last sentence of my last comment?

Sign in to comment.


Utsav Dobhi
Utsav Dobhi on 29 Oct 2020 at 14:00
Hi,
I can not attached the .wam extension file down here. i got an error. Therefore, i transfered the .wam into .txt file.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by