How to extract data from .mat file that contains table

351 ビュー (過去 30 日間)
Valerio Gianforte
Valerio Gianforte 2020 年 3 月 2 日
編集済み: Luna 2022 年 4 月 8 日
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 件のコメント
Valerio Gianforte
Valerio Gianforte 2020 年 3 月 2 日
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.

サインインしてコメントする。

採用された回答

Luna
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
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
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
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.

サインインしてコメントする。

カテゴリ

Find more on Cell Arrays in Help Center and File Exchange

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by