how to properly sort data from a database into a cell structure ?
Show older comments
Hey MATLAB Community,
I have the following database, from which I need to extract some information. It is a database that has a somewhat non-uniform structure. However, the data that I need has a uniform structure so the problem is not very complicated. I am somewhat of a beginner in MATLAB coming from a C++ background so you might see some "MATLAB unfit practices". My goal is:
- to first of all open the database, use textscan to find a structure of strings as you will see in my code,
- go through the newly formed cell structure, find out where there are '1's (as they signal a new data section), retrieve its index and use this index to copy the information present in the cells of the same row
- finally save the whole structure as a new cell structure with organized and seperated data sections
- the final structure will be passed on for further work (not related to this question)
%%Open file and read format in 26 column fields (maximum columns in DB %%
fid = fopen(filename);
% skip the first 2 rows
datacell = textscan(fid, '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','HeaderLines',2);
fclose(fid);
%%find the index of the first column with number 1 to read data section %%
apt_name = strcmp(datacell{1,1},'1');
apt_index = find(apt_name);
apt_index(end+1) = 1; % Here I am trying to add a new element, so I wont exceed my cell dimensions when looping
for i=1:numel(datacell)
for n=1:numel(apt_index)-1
Airport{n}{i} = datacell{1,i}(apt_index(n):(apt_index(n+1)-1)); %<- The problem is here
end
end
As I said my goal is to seperate the data. Ideally I would be able to get a cell structure, that contains seperated data. Sample of the data:
1 21 1 0 KBFI Boeing Field King Co Intl
100 29.87 1 0 0.15 0 2 1 13L 47.53801700 -122.30746100 73.15 0.00 2 0 0 1 31R 47.52919200 -122.30000000 110.95 0.00 2 0 0 1
101 49 1 08 35.04420900 -106.59855700 26 35.04420911 -106.59855711
102 H1 47.53918248 -122.30722302 2.00 10.06 10.06 1 0 0 0.25 0
21 47.53666659 -122.30585255 2 150.28 3.30 13L PAPI-2L
110 1 0.25 150.29 A2 Exit
111 47.53770968 -122.30849802
120 Line B1
111 47.53969864 -122.31276189 51
20 47.54099177 -122.31031317 235.71 0 2
50 12775 ATIS
1 2352 0 0 1RSU 1RSU-NT
100 39.93 4 0 0.25 0 0 0 3 -23.53246610 133.68587381 0.00 0.00 0 0 0 0 21 -23.52755315 133.68954508 0.00 0.00 0 0 0 0
100 86.87 4 0 0.25 0 0 0 5 -23.52883459 133.66776261 0.00 0.00 0 0 0 0 23 -23.51384459 133.69025847 0.00 0.00 0 0 0 0
110 4 0.25 0.00 New Taxiway 15
111 -23.52333339 133.67935332
112 -23.52363409 133.67890961 -23.52
Ideally I would get something like this:
- 1 row cell with 26 column for each line
- n number of rows corresponding to each data section beginning with 1
example
1 21 1 0 KBFI Boeing Field King Co Intl
100 29.87 1 0 0.15 0 2 1 13L 47.53801700 -122.30746100 73.15 0.00 2 0 0 1 31R 47.52919200 -122.30000000 110.95 0.00 2 0 0 1
101 49 1 08 35.04420900 -106.59855700 26 35.04420911 -106.59855711
102 H1 47.53918248 -122.30722302 2.00 10.06 10.06 1 0 0 0.25 0
21 47.53666659 -122.30585255 2 150.28 3.30 13L PAPI-2L
110 1 0.25 150.29 A2 Exit
111 47.53770968 -122.30849802
120 Line B1
111 47.53969864 -122.31276189 51
20 47.54099177 -122.31031317 235.71 0 2
50 12775 ATIS
would have 11 rows and saved in its own cell sections seperated from the rest of the DB info.
I could create dynamic arrays but I have read that it is a no no with MATLAB so I would like to get a way around this issue without any bad practice.
Thank you for your help in advance,
Regards
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!