MATLAB Answers

0

remove header in file

Asked by joo tan on 5 Dec 2011
Latest activity Commented on by chocho
on 18 Feb 2017
latitude, longitude, wind_speed, zonal_wind_speed, meridional_wind_speed, quality_flag
15.25, 94.75, 314, -267, -81, 0
15.25, 95.25, 427, -333, -186, 0
15.25, 95.75, 452, -290, -290, 0
15.25, 96.25, 459, -211, -389, 0
15.25, 96.75, 471, -91, -453, 0
15.25, 97.25, 519, 1, -511, 0
15.25, 97.75, 32767, 32767, 32767, 2
15.25, 98.25, 32767, 32767, 32767, 2
15.25, 98.75, 32767, 32767, 32767, 2
15.25, 99.25, 32767, 32767, 32767, 2
15.25, 99.75, 32767, 32767, 32767, 2
15.25, 100.25, 32767, 32767, 32767, 2
15.25, 100.75, 32767, 32767, 32767, 2
15.25, 101.25, 32767, 32767, 32767, 2
15.25, 101.75, 32767, 32767, 32767, 2
15.25, 102.25, 32767, 32767, 32767, 2

  2 Comments

Sven
on 5 Dec 2011
What's your question? Do you want to read your data into matlab (without the header) or do you want to delete the first line from the file and re-save it?
joo tan on 13 Dec 2011
I want to load the data into matlab without header

Sign in to comment.

Tags

1 Answer

Walter Roberson
Answer by Walter Roberson
on 13 Dec 2011
 Accepted Answer

fmt = repmat('%f',1,6);
fid = fopen('YourFileName.txt', 'rt');
datacell = textscan(fid, fmt, 'Delimiter', ',', 'HeaderLines', 1, 'CollectOutput', 1);
fclose(fid);
TheData = datacell{1};
clear datacell

  15 Comments

Walter Roberson
on 18 Feb 2017
infile = 'COADREAD_methylation.txt';
try
outfile = tempname();
catch ME
error('Your TEMP environment variable is messed up, cannot create temporary file');
end
[fid_in, msg] = fopen(infile, 'r');
if fid_in < 0
error('Could not open input file "%s" because: "%s"', infile, msg);
end
fid_out = fopen(outfile, 'w');
if fid_out < 0
fclose(fid_in);
error('Could not open temporary output file "%s" because: "%s"', outfile, msg);
end
L = fgets(fid_in); %copy first line
fwrite(fid_out, L);
fgets(fid_in); %read next line by do not copy it
while ~feof(fid_in);
L = fgets(fid_in);
fwrite(fid_out, L);
end
fclose(fid_in);
fclose(fid_out);
[pathname, name, ext] = fileparts(infile);
newname = fullfile( pathname, [name '_new' ext] );
try
[success, msg] = movefile( outfile, newname );
catch ME
success = false;
end
if success
fprintf('Done! New file is at "%s"\n', newname );
else
fprintf('Done, but could not move output file here. Look for it at "%s"\n', outfile );
end
Walter Roberson
on 18 Feb 2017
You cannot write to any directory under "C:\Program Files (x86)" because MS Windows will not allow that. You need to cd to a different directory and work there.
chocho
on 18 Feb 2017
oh that's why ,you know i put all the files in this directory C:\Program Files (x86)\MATLAB\R2012a thanks a lot ....

Sign in to comment.