필터 지우기
필터 지우기

How to read the entire column of a file and save it in a variable?

조회 수: 2 (최근 30일)
Dear All I have the below text file (part of it).
=====================================================================
24652 1996-063A ARABSAT-2B
Launched: 1996-11-13 (318) Start Date: 1996-06-12 (164)
Decayed: Stop Date: 2003-12-20 (354)
=====================================================================
1 24652U 96063A 96318.74847837 -.00000020 00000-0 00000+0 0 14
2 24652 3.9929 210.6007 7281127 177.7757 190.4436 2.27277888 06
1 24652U 96063A 96319.62211352 -.00000020 00000-0 00000+0 0 31
2 24652 3.9929 210.3183 7284735 178.4392 185.2995 2.27373269 12
1 24652U 96063A 96319.62351606 .00008082 00000-0 30835-2 0 24
2 24652 3.9764 210.1654 7280836 178.5436 186.6267 2.27380102 20
1 24652U 96063A 96319.62356237 .00009638 00000-0 38025-2 0 37
2 24652 3.9632 210.3512 7280110 178.4006 186.6625 2.27374993 25
1 24652U 96063A 96320.05952563 -.00002597 00000-0 -98092-3 0 63
2 24652 3.9623 210.1661 7275699 178.7092 185.6294 2.27896863 39
1 24652U 96063A 96320.49676119 -.00000127 00000-0 10000-4 0 72
I am using the following code to read the each line.
fid=fopen('2B.txt');
A=textscan(fid,'%s','HeaderLines',5);
Data=reshape(A{1}(1:end-3,:),9,[])';
fclose(fid);
l1=Data(1:2:end,:);
l2=Data(2:2:end,:);
I want to read all the elements of line 1 (l1) column 4(i.e. 96318.74847837, 96319.6221135296, and so on..)
I tried t=l1(:,4) but it doesn't work. It only displays 96318.74847837
please help.
  댓글 수: 2
per isakson
per isakson 2012년 9월 22일
What is this "00000-0 00000+0"?
Hamza
Hamza 2012년 9월 23일
It's basically a TLE data(Two line element) and those '0's refer to drag term and 2nd derivative of mean motion.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 22일
편집: Azzi Abdelmalek 2012년 9월 22일
fid=fopen('2B.txt');
A=textscan(fid,'%s','HeaderLines',5);
fclose(fid);
n=numel(A{:});
Data=reshape(A{1},9,[])';
l1=Data(1:2,:);
l2=Data(2:2,:);
t=l1(:,4:end)
%or using fgetl.
fid = fopen('2B.txt');
tline = fgetl(fid);
out=tline
while ischar(tline)
tline = fgetl(fid);
out=char(out,tline)
end
fclose(fid);
n=size(out,1);
res=[]
for k=6:n
r=regexp(out(k,:),' ','split')
r1=cell2mat(cellfun(@(x) ~isempty(x),r,'Uni',false))
r2=r(r1)
res=[res;cellfun(@(x) str2num(x),r2(4:end))]
end
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 23일
It's not t=l(:,4) but t=l(:,4:end)
Hamza
Hamza 2012년 9월 24일
I was talking about my previous code that t=l1(:,4) is now displaying all the elements of 4th column. Which it wasn't displaying previously.

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

추가 답변 (1개)

bym
bym 2012년 9월 22일
try using this
A=textscan(fid,repmat('%s',1,9),'HeaderLines',5);
>> A{4}(1:2:end)
ans =
'96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by