Import data as a structure array from a text file with different titles in the middle.
24 views (last 30 days)
Show older comments
Hi,
I have this kind of a data file (I have attached the file here), for different time values particle cordinates and other variables. I want to import this data to matlab as structure array, Ex: for differenent time values x, y, z columns. Attached file has x, y, z,...data for 2 time values. My original file has this for 2796 time values (huge file).
So far I have tried this but it didn't work. And I can't understand how to use "struct" here, 1*2796 struct with 4 fields (number of particles (50), x, y, z) . My ultimate goal is to make a movie with these particle trajectoties.
pardist = load('datafile.txt');
for t=-3.555e-11:2e-11:5.590e-8 %this is the time frame.
for k=0:2796 % how many times
k1=3+3*k+50*k; % to get the correct row number; as I have time and column name in the middle.
data(t)=pardist(:,k1:k1+49)
end
end
Could you please help me with importing this data to a structure array. Thank you very much.
Accepted Answer
Stephen23
on 6 Apr 2020
Edited: Stephen23
on 6 Apr 2020
opt = {'MultipleDelimsAsOne',true};
out = {};
[fid,msg] = fopen('datafile.txt','rt');
assert(fid>=3,msg)
while ~feof(fid)
str = fgets(fid);
val = sscanf(str,'time%f');
if numel(val)
hdr = regexp(fgets(fid),'\w+','match');
fmt = repmat('%f',1,numel(hdr));
tmp = textscan(fid,fmt,opt{:});
tmp = cell2struct(tmp,hdr,2);
tmp.time = val;
out{end+1} = tmp;
end
end
fclose(fid);
out = [out{:}]; % All structures must have the same fields!
Checking the output structure:
>> out
out =
1x2 struct array containing the fields:
x
y
z
rxy
G
Bx
By
Bz
m
q
nmacro
rmacro
ID
fEx
fEy
fEz
fBx
fBy
fBz
time
>> out(1).time
ans = -0.000000000035550
>> out(1).x
ans =
-0.000671100000
0.000557200000
-0.000973600000
0.000186800000
0.000594100000
0.000012390000
0.001282000000
-0.000289500000
-0.001298000000
0.000587800000
-0.002283000000
0.000353100000
0.001420000000
-0.000765300000
0.000874900000
-0.000914000000
0.000142200000
-0.000250900000
0.000243000000
0.001025000000
-0.000179200000
0.002040000000
-0.000258000000
-0.001343000000
0.000287200000
-0.001529000000
0.000887600000
0.002393000000
-0.000354500000
0.000134900000
-0.000845700000
-0.000897900000
0.000972200000
-0.000000027500
0.000247200000
-0.000262000000
0.001278000000
-0.000545300000
-0.002156000000
0.000313400000
-0.001397000000
0.000723800000
0.001591000000
-0.000895500000
0.000795900000
-0.000517700000
-0.000153600000
0.000869200000
-0.000573800000
0.000658400000
>> out(2).G
ans =
1.1620
1.1350
1.1280
1.1170
1.1120
1.1050
1.1010
1.0950
1.0930
1.0880
1.0860
1.0820
1.0790
1.0760
1.0740
1.0710
1.0660
1.0650
1.0620
1.0600
1.0580
1.0570
1.0540
1.0530
1.0510
1.0490
1.0470
1.0460
1.0440
1.0420
1.0400
1.0390
1.0370
1.0340
1.0330
1.0300
1.0290
1.0270
1.0260
1.0240
1.0230
1.0210
1.0190
1.0170
1.0160
1.0130
1.0120
1.0090
1.0080
1.0040
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!