How to load data with using detectimportsoption

1 Ansicht (letzte 30 Tage)
Utsav Dobhi
Utsav Dobhi am 28 Okt. 2020
Beantwortet: Utsav Dobhi am 29 Okt. 2020
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', ',')

Antworten (2)

Image Analyst
Image Analyst am 28 Okt. 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 Kommentare
Utsav Dobhi
Utsav Dobhi am 29 Okt. 2020
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 am 29 Okt. 2020
Did you overlook the last sentence of my last comment?

Melden Sie sich an, um zu kommentieren.


Utsav Dobhi
Utsav Dobhi am 29 Okt. 2020
Hi,
I can not attached the .wam extension file down here. i got an error. Therefore, i transfered the .wam into .txt file.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by