Info

此问题已关闭。 请重新打开它进行编辑或回答。

Read out a textfile -> search for special characters and save the value after it -> and save them to the corresponding rows.

1 次查看(过去 30 天)
Hey guys i have a problem. i have a file which looks like the following one. Here i read out the two rows for each measurement. In this case I have 3. That works perfectly for me. But i also need the time which is written like "t = 0s". so for each measurement in this file the time should be saved with the corresponding two rows.
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.

回答(1 个)

Bob Thompson
Bob Thompson 2018-5-7
Generally I use fgetl() to extract data from a mixed string and number line. It's not the most efficient I'm sure, but it has been working for me.
while ~isnumeric(line)
line = fgetl(fid);
count = count + 1;
if strcmp(line(8:10),'t =') == 1;
timestamp = str2num(line(12:end)); % You're going to have to mess with this
% because of the 's' at the end.
end
end
This is by no means a perfect setup, but it should give you an idea of the application.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by