How to extract data from .mat file that contains table
Show older comments
Hi everyone,
I would like extract data from table on the .mat file. I tried to convert the 'struct' into 'cell' obtaining a cell array 1x1 and then I tried to convert 'cell' into numeric array but cell2mat doesn't support cell array. The goal is to obtaine a matrix of dimension 30932x5. Are there someone that can help me? Thanks.
fileName = 'D:\Valerio\data\data_models\ACCESS1_0.mat';
loadfile = load(fileName);
table = struct2cell(loadfile);
data_matrix = cell2mat(table);
data_matrix = cell2mat(table)
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
4 Comments
Valerio Gianforte
on 2 Mar 2020
Luna
on 2 Mar 2020
Your table's first column is a date variable. If you want to get Nx5 you will definetely lose the time column. It is not possible to create double matrix with different types of variables. You can get Nx5 cell array (because cell arrays can store different types of variables in each cell), but you will get Nx4 double matrix. What do you want to do exactly?
Valerio Gianforte
on 2 Mar 2020
Accepted Answer
More Answers (1)
A structure and a table are two completely different things in matlab.
Now, loadfile is always going to be a structure. The fields of that structure are going to be the variables of the mat file. You would rarely convert that structure into a cell array as you'd loose the variable names and would be relying solely on the variable ordering to get the correct data out the mat file.
If you want to get assign a particular variable of the mat file to another (non-struct) variables, this will be:
loadfile = load(fileName);
yourvarname = loadfile.actualvariablename;
If that variable is a table, then you could possibly convert it to a table with table2array or plain {} indexing. In both cases, the table must only contain numerical values of the same class.
edit: since you've now attached your mat file:
loadfile = load(fileName);
table_def = loadfile.table_def;
will get you the table into its original name.
"I want to extract data column by column because I must manipulate them"
Most likely the wrong approach. It's very much possible that what you want to do can be done in just a few lines as long as the data is kept together in the table (or better converted to a timetable). You haven't really explained what you want to do though.
2 Comments
Valerio Gianforte
on 2 Mar 2020
"I have four sectors that are inside the fourth column (North, East, West, South)"
Wouldn't these be the HH column (2nd column of the table). That's the only column of your table that has 4 unique values.
"associated values of height and period of waves and also dates"
date is clearly the YYMMHH variable, which of Hs_tot, Tm_tot, Dm_tot is which?
" I should to do a graph with x-y axes where x is the time and y is the height"
Do I understand correctly that you want to plot one of the above 3 variable against time for each sector? So, you'd have 4 lines on the same graph. If so, splitting the table into individual variables is the wrong approach. The plotting for all quadrants can be done in just one line.
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!