How to extract data from .mat file that contains table

309 views (last 30 days)
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
Luna
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
Valerio Gianforte on 2 Mar 2020
I want to extract data column by column because I must manipulate them and then I have to save them in another table. I need of all columns, also datetime. After manipulating, I must do a graph with the time (I'll convert with the datetime command) and the third column.

Sign in to comment.

Accepted Answer

Luna
Luna on 2 Mar 2020
Edited: Luna on 8 Apr 2022
Hi,
Here is my answer for your question. Please read my comments near each line.
table_loaded = load('C:\Users\...\Downloads\ACCESS1_0.mat'); % loaded structure
your_actual_table = table_loaded.table_def; % this struct already have a table_def field and that field contains a table.
your_timetable = table2timetable(table_loaded.table_def); % You can convert it to timetable.
%% Once you get your table you can reach each column with dot notation like below:
date_array = your_timetable.YYMMDD; % type is datetime
H_array = your_timetable.HH; % type is int32
Hs_tot_array = your_timetable.Hs_tot; % type is single
Tm_tot_array = your_timetable.Tm_tot; % type is single
Dm_tot_array = your_timetable.Dm_tot; % type is single
% Now manipulate them and make some convert operations because you can't
% concatanate int32, single and datetime into a double matrix.
%% For example:
H_array_converted = double(H_array); % now it is type double.
% if you don't do conversion, you can't even make mathematical operations. For example try to sum 2nd elements of arrays:
H_array(2) + Hs_tot_array(2) % this will give error. Because you can't sum int32 and single.
  3 Comments
Luna
Luna on 3 Mar 2020
Please accept answer if it is a solution :)
Eoghan Mckay
Eoghan Mckay on 25 Mar 2022
Hats off to you for commenting on each line. Makes it so much easier to learn and follow your thought process.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 2 Mar 2020
Edited: Guillaume on 2 Mar 2020
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
Valerio Gianforte on 2 Mar 2020
Ok, I try me to explain better. I have four sectors that are inside the fourth column (North, East, West, South), for each range of sectors I must look for the associated values of height and period of waves and also dates. After all of that I should to do a graph with x-y axes where x is the time and y is the height. I don't know if is clearer than before but I wouldn't know how to explain it better.
Guillaume
Guillaume on 2 Mar 2020
Edited: Guillaume on 3 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.

Sign in to comment.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!