How to extract data from .mat file that contains table
137 次查看(过去 30 天)
显示 更早的评论
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 个评论
Luna
2020-3-2
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?
采纳的回答
Luna
2020-3-2
编辑:Luna
2022-4-8
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 个评论
Eoghan Mckay
2022-3-25
Hats off to you for commenting on each line. Makes it so much easier to learn and follow your thought process.
更多回答(1 个)
Guillaume
2020-3-2
编辑:Guillaume
2020-3-2
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 个评论
Guillaume
2020-3-2
编辑:Guillaume
2020-3-3
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!