How to extract data from .mat file that contains table
조회 수: 182 (최근 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!