MATLAB: Automated data parsing into structure

arraydataimportMATLABparsereadsavestorestructurestructures

Hello everyone,
I have a pretty large data set with 4 fields for each set. I would like to access this data, parse the individual fields, and store them into a structure without losing the correlation between the sets.
To show the data so you know what I'm talking about:
3298602123321 1 2011-09-20 01:00:00 10.8554401397705
3298602123321 1 2011-09-20 01:15:00 10.8555603027344
3298602123321 1 2011-09-20 01:30:00 10.8555603027344
3298602123321 1 2011-09-20 01:45:00 10.8560495376587
As you can see, there are four independent fields on each line, I need to be able to access a certain timestamp, and pull data that corresponds to that timestamp, without jumbling the entire operation.
Here is some of the code I have so far:
C=cell(numberoflines+1,5);
C(1,1)={'Counts'};
C(1,2)={'sensor_ID'};
C(1,3)={'point_ID'};
C(1,4)={'sampling_time'};
C(1,5)={'sensing_value'};
FID=fopen('C:/users/desktop/2.txt','r');
Counts=1;
while Counts <= numberoflines
Tline =fgetl(FID);
sensor_ID =Tline(1:13);
point_ID =Tline(22);
sampling_time =Tline(34:52);
sensing_value =Tline(94:110);
C(Counts+1,1)={Counts};
C(Counts+1,2)={sensor_ID};
C(Counts+1,3)={point_ID};
C(Counts+1,4)={sampling_time};
C(Counts+1,5)={sensing_value};
Counts=Counts+1;
end
fclose(FID);
rowHeadings = {'Counts', 'sensor_ID', 'point_ID', … 'sampling_time', 'sampling_value'};
Data = cell2struct(C, rowHeadings, 2)
What I have done with this code is access the .txt file containing my information, created an empty cell array with headers, parse the information line by line, assign to the cell array, then convert the cell array to a structure.
My problem is this: I can not figure out how to keep everything grouped together so that by recalling one item automatically brings the corresponding items in the set to where I need them.
If anyone can help, I would be extremely grateful, thanks for taking the time to read this, thanks in advance for any assistance. Have a great day!
~Sarah 🙂

Best Answer

Assume there are no blank lines in your original text file, you could do this. All the data is stored as string, but won't be hard to convert to numerical data.
FID=fopen('test.txt','r');
C=textscan(FID,'%s %s %s %s %s');
fclose(FID);
D=struct('sensor_ID',C{1},...
'point_ID',C{2},...
'sampling_date',C{3},...
'sampling_time',C{4},...
'sensing_value',C{5});