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 件のコメント

Sven
Sven 2011 年 12 月 5 日
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
joo tan 2011 年 12 月 13 日
I want to load the data into matlab without header

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 13 日

0 投票

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 件のコメント

chocho
chocho 2017 年 2 月 18 日
編集済み: Walter Roberson 2017 年 2 月 18 日
i have file like that but i want to remove the second header line as follow:
Hybridization REF TCGA-A6-2672-11A-01D-1551-05 TCGA-A6-2672-11A-01D-1551-05
Composite Element REF Beta_value Gene_Symbol Chromosome Genomic_Coordinate
cg00000292 0.511852232819811 ATP2A1 16 28890100
i want to delete the second headerline (Composite Element REF Beta_value Gene_Symbol Chromosome Genomic_Coordinate)
Walter Roberson
Walter Roberson 2017 年 2 月 18 日
fmt = '%s%f%s%f%f';
fid = fopen('YourFileName.txt', 'rt');
header_to_keep = fgetl(fid);
datacell = textscan(fid, fmt, 'Headerlines', 1);
fclose(fid);
Composite_Element_Ref = datacell{1};
Beta_value = datacell{2};
Gene_Symbol = datacell{3};
Chromosome = datacell{4};
Genomic_Coordinate = datacell{5};
together with the whatever you want to do with the header_to_keep line, which you do not appear to want to delete.
If instead you wanted to delete both header lines, then
fmt = '%s%f%s%f%f';
fid = fopen('YourFileName.txt', 'rt');
datacell = textscan(fid, fmt, 'Headerlines', 2);
fclose(fid);
Composite_Element_Ref = datacell{1};
Beta_value = datacell{2};
Gene_Symbol = datacell{3};
Chromosome = datacell{4};
Genomic_Coordinate = datacell{5};
chocho
chocho 2017 年 2 月 18 日
編集済み: Walter Roberson 2017 年 2 月 18 日
my fmt i more biger than that i just post an example among them
i used getl to get the whole lines of my text file , then i want to delete the whole lines 2
you get it ?
fid = fopen('COADREAD_methylation.txt','r');
data={};
while ~feof(fid)
l=fgetl(fid);
if isempty(strfind(l,'NA')), data=[data;{l}]; end
end
fid=fclose(fid);
Walter Roberson
Walter Roberson 2017 年 2 月 18 日
編集済み: Walter Roberson 2017 年 2 月 18 日
fid = fopen('COADREAD_methylation.txt','r');
data={};
linecount = 0;
while ~feof(fid)
l = fgetl(fid);
linecount = linecount + 1;
if linecount == 2; continue; end %skip line 2 specifically
if isempty(strfind(l,'NA'))
data{end+1} = l;
end
end
fid = fclose(fid);
... Or you could just use
data = strsplit( fileread('COADREAD_methylation.txt'), '\n' );
data{2} = [];
chocho
chocho 2017 年 2 月 18 日
hi friend i tried that but it seemes there is an error if linecount == 2; next; end %skip line 2 specifically??
Walter Roberson
Walter Roberson 2017 年 2 月 18 日
Sorry, should be continue instead of next.
chocho
chocho 2017 年 2 月 18 日
Undefined function or variable 'next'.
chocho
chocho 2017 年 2 月 18 日
sorry again if i bother you but now no error but the code doesn't delete the line 2 !!! still exist in the textfile
Walter Roberson
Walter Roberson 2017 年 2 月 18 日
If you want to remove a line from the file itself, then you have to write out a new file.
fid_in = fopen('COADREAD_methylation.txt', 'r');
fid_out = fopen('new_file.txt', 'w');
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);
After that you would check to be sure everything worked, and then you could rename the old file to a safe place and rename the new file to the old name.
chocho
chocho 2017 年 2 月 18 日
so sorry , again error Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Untitled (line 4) fwrite(fid_out, L);
chocho
chocho 2017 年 2 月 18 日
編集済み: Stephen23 2017 年 2 月 18 日
on brief ,the text file is very large 22198x1 cell i want to remove the second line why you use fgets instead of fgetl !
chocho
chocho 2017 年 2 月 18 日
i mean it's a cell array of size 27581*1 with NA without NA 22198*1
Walter Roberson
Walter Roberson 2017 年 2 月 18 日
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
Walter Roberson 2017 年 2 月 18 日
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
chocho 2017 年 2 月 18 日
oh that's why ,you know i put all the files in this directory C:\Program Files (x86)\MATLAB\R2012a thanks a lot ....

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2011 年 12 月 5 日

コメント済み:

2017 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by