Data Parsing

18 views (last 30 days)
Robbie
Robbie on 24 Feb 2012
Answered: Sushranth on 24 Jun 2021
I have some data in the following format:
0.100000000000000 0.750000000000000 0.229230000000000
0.200000000000000 0.750000000000000 0.249220000000000
0.200000000000000 0.760000000000000 0.257400000000000
0.200000000000000 0.770000000000000 0.266600000000000
0.200000000000000 0.780000000000000 0.277160000000000
0.200000000000000 0.790000000000000 0.289840000000000
0.200000000000000 0.800000000000000 0.307550000000000
0.400000000000000 0.750000000000000 0.289260000000000
0.400000000000000 0.760000000000000 0.299330000000000
0.400000000000000 0.770000000000000 0.310670000000000
0.400000000000000 0.780000000000000 0.325160000000000
0.400000000000000 0.790000000000000 0.339120000000000
0.400000000000000 0.800000000000000 0.356690000000000
0.600000000000000 0.740000000000000 0.319580000000000
0.600000000000000 0.750000000000000 0.329360000000000
0.600000000000000 0.760000000000000 0.341200000000000
0.600000000000000 0.770000000000000 0.355150000000000
0.600000000000000 0.780000000000000 0.370290000000000
0.600000000000000 0.790000000000000 0.389880000000000
0.600000000000000 0.800000000000000 0.409590000000000
0.800000000000000 0.740000000000000 0.358500000000000
0.800000000000000 0.750000000000000 0.370750000000000
0.800000000000000 0.760000000000000 0.384590000000000
0.800000000000000 0.770000000000000 0.400020000000000
0.800000000000000 0.780000000000000 0.420100000000000
0.800000000000000 0.790000000000000 0.433160000000000
0.800000000000000 0.800000000000000 0.456660000000000
What I want to be able to do is split this data in a 'row-wise' sense by putting it into a struct. There are multiple occurences of values in the first column and I want to extract them and the other data associated with those in the other columns. For example I want:
Output.'0.1' Output.'0.2' etc....
In Output.'0.2' then for example I would get the following data:
0.200000000000000 0.750000000000000 0.249220000000000
0.200000000000000 0.760000000000000 0.257400000000000
0.200000000000000 0.770000000000000 0.266600000000000
0.200000000000000 0.780000000000000 0.277160000000000
0.200000000000000 0.790000000000000 0.289840000000000
0.200000000000000 0.800000000000000 0.307550000000000
Any suggestions are greatly appreciated.
Thanks

Answers (1)

Sushranth
Sushranth on 24 Jun 2021
It is my understanding that you want to establish a key - value relationship in your dataset where the first column corresponds to the keys. A Map container might be better suited for your use case than a struct.
I will assume that the dataset is stored as a matrix.
%Let t be a matrix that contains the data.
keys = unique(t(:,1)); %Extract unique values from the first column
values = {};
for k=1:length(keys) %Iterate over all the keys
key = keys(k);
row_idx = (t(:,1) == key); %Find the row index in the data that corresponds to the key.
values{k} = t(row_idx,:); % Extract those rows from the data and add it to the values cell array.
end
M = containers.Map(keys,values); % Use the keys and values to create a Map
For more details on the Map container, refer to the below documentation :-

Community Treasure Hunt

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

Start Hunting!

Translated by