Importing periodic data with textscan and fgetl?

4 次查看(过去 30 天)
Here's a subset of the data I'm trying to import into MATLAB:
Time = 0.50734E-01
X Y Z EP_g
23.865 25.227 -0.50000 0.56828
24.135 25.227 -0.50000 0.56839
23.865 25.500 -0.50000 0.56834
24.135 25.500 -0.50000 0.56831
Time = 1.00059E-01
X Y Z EP_g
23.865 25.227 -0.50000 0.56828
24.135 25.227 -0.50000 0.56840
23.865 25.500 -0.50000 0.56835
24.135 25.500 -0.50000 0.56832
I am trying to import this data using textscan and fgetl but have been having trouble. The problem is those pesky lines between each subset.
The column on the far right (EP_g) is the only thing I'm really interested in. By the time I'm done, I just want a matrix with the transpose of the EP_g column along the rows, i.e.,
data =
0.56828 0.56839 0.56834 0.56831
0.56828 0.56840 0.56835 0.56832
My attempt at a solution:
fid=fopen(file)
while ~feof(fid)
tmp=textscan(fid,'%f%f%f%f','Headerlines',5)
for j=[1:2]
fgetl(fid)
end;
end;
fclose(fid);
I would appreciate any guidance or help you can give me. Many thanks!

采纳的回答

William
William 2012-11-21
I managed to get it to work by using only the textscan function. I had seen an example using fgetl and I thought I needed it for this application, but it turns out I didn't.
fid=fopen('n90deg'); % Open file
c=1; % Initialize counter
while ~feof(fid); % Run until end of file
tmp=textscan(fid,'%f%f%f%f','Headerlines',2); % Omit first two lines every
% time
n90deg(c,:)=tmp{4}'; % Store the last cell as a row in new matrix
c=c+1; % Add to counter and repeat above steps
end
fclose(fid);
Hopefully someone else will find this useful.

更多回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-11-21
fid = fopen('file.txt');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2012-11-21
编辑:Azzi Abdelmalek 2012-11-21
fid = fopen('data3.txt');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);
n=size(res,1)-1
ii=0;
for k=1:6:n
ii=ii+1
data{ii}=str2num(res(k+2:k+5,:))
t=regexp(res(k,:),'=','split')
t1{ii}=str2num(t{2})
end
William
William 2012-11-21
Yes, that makes much more sense. I was trying str2mat and str2cell but didn't try str2num. Thank you very much Azzi!

请先登录,再进行评论。


Y. J.
Y. J. 2018-5-7
Hey guys i have a simmilar problem. My File looks like this:
06.12.2099 13:50:37
502C43 ZU152323
Radial
f acceleration
b kg
Example value
1/3 t = 0 s
+0.0000e+00 +1.4013e-03
+1.4648e+02 +2.6760e-05
+2.9297e+00 +2.1846e-06
+4.3945e+04 +1.5014e-10
+5.8594e+00 +7.3407e-40
+7.3242e+06 +6.7897e-06
2/3 t = 0.682667 s
+0.0000e+00 +1.4013e-03
+1.4648e+02 +2.6760e-05
+2.9297e+00 +2.1846e-06
+4.3945e+04 +1.5014e-10
+5.8594e+00 +7.3407e-40
+7.3242e+06 +6.7897e-06
3/3 t = 0.682667 s
+0.0000e+00 +1.4013e-03
+1.4648e+02 +2.6760e-05
+2.9297e+00 +2.1846e-06
+4.3945e+04 +1.5014e-10
+5.8594e+00 +7.3407e-40
+7.3242e+06 +6.7897e-06
And i read the two value columns like this:
fmt = '%f %f';
fid=fopen('Example.txt,'r');
k = 1;
while (~feof(fid))
k = k+1;
c(k-1,1) = textscan(fid,fmt,'collectoutput',true,'HeaderLines',7);
end
fid=fclose(fid);
This works fine and he stores me the Values for each time separately. But I need also the time value after "t = ". And it should be saved in my cell also. at the moment i get a 3x1 cell (in this case).
Thank you in advance.

类别

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