how to properly sort data from a database into a cell structure ?

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

Coming from C++, I would think it is obvious that dynamic variable names are a no no. It's not even possible in C++. In any case, I don't think how it is even relevant to your problem.
I'm really not clear on what you're trying to obtain. You seem to be describing two different kind of output, one where each line is a row of a cell array and each string of the line a column. That's easy to obtain:
wholecontent = fileread(filename); %ideally filename is the name + path to avoid relying on current directory
filelines = strsplit(wholecontent, {'\n', '\r'}); %works on both linux and windows
splitlines = cellfun(@strsplit, filelines, 'UniformOutput', false);
But you're also talking about identifying each section starting with a '1' string. In which case:
wholecontent = fileread(filename); %ideally filename is the name + path to avoid relying on current directory
filestrings = strsplit(wholecontent); %split on whitespaces
sectionstarts = find(strcmp(filestrings, '1'));
sections = mat2cell(filestrings, 1, diff([sectionstarts, numel(filestrings)+1]));
In each case the output is a 1D cell array of 1D cell array of different lengths. If you want a 2D cell array with empty elements for padding instead:
function cell2d = mergesubcell(cell1d)
%MERGESUBCELL merge a 1D cell array of 1D (row) cell arrays into a 2D cell array, padding with empty cells if necessary
assert(iscell(cell1d) && isvector(cell1d), 'Input is not a vector cell array');
assert(all(cellfun(@(v) iscell(v) && isrow(v), cell1d)), 'Input is not a cell array of row cell arrays');
maxlength = max(cellfun(@numel, cell1d));
cell2d = cellfun(@(v) [v, cell(1, maxlength - numel(v))], cell1d, 'UniformOutput', false);
cell2d = vertcat(cell2d{:});
end

6 Comments

thank you, yes I did not convey precisely what I meant. In dynamic variable names I meant as initialising variable with similar name such as foo1,foo2,foo3,....foon. Which is not really dynamic, since I am giving it a finite number. I will try your code and understand what I did wrong to learn from the mistakes I did(giving it works without a problem and serves its purpose) and will accept your answer of course.
This is not exactly what I had in mind, but it is a step closer. I do not want 2 different outputs, if you reread the question I gave you the format of the output. Anyways the mergesubcell code you gave me is great in combination with the first section of code. The second section was not really what I had in mind, but I have learned a new way of reading in data and manipulating it through mat2cell.
This is a very solid answer, I will wait for a few other responses. If this is the best one, I will accept it as an answer.
Thank you again
Numbered variables are indeed a big no no. You wouldn't have that in C++ and you shouldn't have that in matlab. You'd store that in a vector / map / list / whichever container is appropriate. Same in matlab: cell array or matrix
As said, I don't understand what you want as an output. I don't see an example of output, certainly not something in matlab syntax. With the example of input you gave, can you show the exact output you want?
Hallo Guillaume,
What I would really like to do is:
after reading in the '.dat' file which has the sample data
Input:
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
output:
Airport1{1,1} Airport1{1,2} Airport1{1,3} Airport1{1,4} Airport1{1,5} Airport1{1,6} ......
1 21 1 0 KBFI Field King
Airport1{2,1} Airport1{2,2} Airport1{2,3} Airport1{2,4} Airport1{2,5} Airport1{2,6} ......
100 29.87 1 0 0.15 0 2
.......................................................................
.......................................................................
.......................................................................
Airport2{1,1} Airport2{1,2} Airport2{1,3} Airport2{1,4} Airport2{1,5} Airport2{1,6} ......
1 2352 0 0 1RSU 1RSU-NT
Airport2{2,1} Airport2{2,2} Airport2{2,3} Airport2{2,4} Airport2{2,5} Airport2{2,6} ......
100 39.93 4 0 0.25 0 0 ......
....................................................................
....................................................................
....................................................................
I have tried to do this with a simple for loop but I get empty cells as an output.
Thank you again
Now I understand, The '1' sections starts when the '1' is at the beginning of a line. Then it's fairly easy, reuse the first part of my answer:
wholecontent = fileread(filename); %ideally filename is the name + path to avoid relying on current directory
filelines = strsplit(wholecontent, {'\n', '\r'}); %works on both linux and windows
splitlines = cellfun(@strsplit, filelines, 'UniformOutput', false);
Merge the whole lot into a 2D cell array using the mergesubcell function that I wrote
airport = mergesubcell(splitlines);
Identify the '1' in the first column and use that to split the cell array:
sectionstarts = find(strcmp(airport(:, 1), '1'));
airports = mat2cell(airport, diff([sectionstarts; size(airport, 1) + 1]), size(airport, 2))
And you get a cell array (instead of numbered variables which as said is not a good idea) of airport cell arrays. Loops not needed.
worked like a charm. I think that MATLAB isn't very intuitive with its structure, it is so simple yet very different from other programming languages.
Why can't I loop through a cell and copy cells directly ? As I understood, it has to be a mat before it is transformed into a cell structure. Is it because MATLAB is optimized for mat operations ? or can I just directly take values from cells and modify or copy them ? It would be helpful for future programs.

Sign in to comment.

More Answers (0)

Asked:

on 24 Oct 2016

Commented:

on 26 Oct 2016

Community Treasure Hunt

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

Start Hunting!