Automated data parsing into structure

10 views (last 30 days)
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 :)

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 29 Sep 2011
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});
  3 Comments
Fangjun Jiang
Fangjun Jiang on 30 Sep 2011
The result of this is a structure array. Any time you just need to provide an index to reference the corresponding value, such as D(1).point_ID, D(1).sample_time
>> D
D =
4x1 struct array with fields:
sensor_ID
point_ID
sampling_date
sampling_time
sensing_value
>> D(1)
ans =
sensor_ID: '3298602123321'
point_ID: '1'
sampling_date: '2011-09-20'
sampling_time: '01:00:00'
sensing_value: '10.8554401397705'
>> D(4)
ans =
sensor_ID: '3298602123321'
point_ID: '1'
sampling_date: '2011-09-20'
sampling_time: '01:45:00'
sensing_value: '10.8560495376587'
>> D(1).point_ID
ans =
1
>> D(1).sampling_time
ans =
01:00:00
Sarah
Sarah on 30 Sep 2011
Thank you for your help Fangjun! I was not seeing the method behind everything, I appreciate it greatly.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!