How to create a dataset array from table?
Show older comments
Hi evereyone,
I have a large table (3824x97) and everyday becomes larger. My table (VESSEL_DATA) has 97 variables name (first row) and i want to convert it in dataseet array. So i use :
xl_path=strcat(cd,'\','vessels_data','.xlsx');
xlswrite(xl_path,VESSEL_DATA);
Vessel_Data=dataset('XLSFile',xl_path);
It works but it takes too long. So I want to ask you if there is a different way to create my dataset directly, without excel help.
Tks & Brgds
Accepted Answer
More Answers (2)
Paul Peeling
on 13 Dec 2011
Hi Alexis
You should be able to create a dataset directly from your table, without writing and reading from Excel. Once you have your variable names in a cell array of strings called VarNames, you create the dataset with this syntax:
Vessel_Data = dataset({VESSEL_DATA(2:end,:),VarNames{:}});
Regards
3 Comments
alexis
on 14 Dec 2011
Paul Peeling
on 14 Dec 2011
Hi Alexis
I'm glad to help. Tell me about the VESSEL_DATA table. You mentioned that the first row contains the column names. What is in the rest of the table? Is VESSEL_DATA a 2D cell array of strings
Thanks
Paul
alexis
on 15 Dec 2011
owr
on 14 Dec 2011
I think Paul is on the right track but the syntax is slightly off.
If "Vessel_Data" looks like this:
>> Vessel_Data = {'Col1','Col2';'A','D';'B','E';'C','F'}
Vessel_Data =
'Col1' 'Col2'
'A' 'D'
'B' 'E'
'C' 'F'
(note the column headers in row 1)
Then this call to dataset will get you what I think you are looking for:
>> dataset([{Vessel_Data(2:end,:)},Vessel_Data(1,:)])
ans =
Col1 Col2
'A' 'D'
'B' 'E'
'C' 'F'
Its a bit confusing, but pick apart each step and you'll understand. You are basically passing the dataset array constructor one cell array with 2 elements. The first is the data itself, in this case a 3x2 cell array (could easily have been numeric data), the second is a cell array with column headers - one for each column in the data (in this case 2 columns).
Hope this helps. It took me awhile to get this right when I needed it as well.
3 Comments
alexis
on 15 Dec 2011
owr
on 15 Dec 2011
If the data is already loaded in the MATLAB in a cell array and some of the columns contain chars and others contain doubles you will need to separate them before creating the dataset array. An alternative is to load the table straight from a file like you originally tried to do with Excel. It may be faster to use a CSV file.
Where does your data originally come from?
Post a small example of what your table looks like. Just copy and paste it from the desktop.
Without knowing these 2 things I can't help you further.
alexis
on 19 Dec 2011
Categories
Find more on Tables 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!