How to create a dataset array from table?
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
Paul Peeling
2011년 12월 13일
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
alexis
2011년 12월 14일
Paul Peeling
2011년 12월 14일
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
2011년 12월 15일
owr
2011년 12월 14일
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
alexis
2011년 12월 15일
owr
2011년 12월 15일
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
2011년 12월 19일
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!