How to transform a table to a matrix?
45 views (last 30 days)
Show older comments
I have an excel file that I import to matlab by:
data = 'spatialcurve.xls';
flux = readtable(data,'PreserveVariableNames',true);
flux = table2array(flux);
Unable to concatenate the table variables '2023-08-16 13:49:49.151' and 'Var7', because their types are cell and double."
If I use:
data = importdata('spatialcurve.xls');
flux = struct2cell(data);
flux = cell2mat(data);
All contents of the input cell array must be of the same data type."
I can slove it by using xlsread but since this is not recommended anymore (for some reasons I don't know), I wonder if it is possible to do it with readtable or importdata?
2 Comments
Dyuman Joshi
on 1 Sep 2023
To create (or convert to) a numeric matrix, all the elements must be numeric scalars (or numerical arrays of compatible dimensions for concatenation), which, by looking at the errors, is not the case with your data.
You can either store hetergeneous data in a cell array or a table (or a struct).
Could you please attach the excel? Use the paperclip button to do so.
Accepted Answer
dpb
on 1 Sep 2023
xlsread has been deprecated because it is more difficult to use with irregular data than the table.
You can't put different data types into a single array other than a cell array, no matter how you read it; it's not reading that's the issue here; it is that you have two (at least) different data types that rightfully should remain as such.
The solution to the problem is to not continue to try to go down an impossible path but use the table you just read and <address the variables from it directly>. Given that one is a datetime, perhaps it would make more sense to use a timetable instead; it has certain additional functionality that can be quite helpful with time-based data.
6 Comments
dpb
on 4 Sep 2023
Edited: dpb
on 4 Sep 2023
<The R2019b doc> says it exists so something else must be going on...double check spelling including an inadvertent hidden character or somesuch.
Well, the functionality exists, it had a different name/syntax then, it is
'PreserveVariableNames',|true/false| instead; I whiffed on the name change earlier, sorry.
More Answers (2)
Dyuman Joshi
on 1 Sep 2023
Edited: Dyuman Joshi
on 1 Sep 2023
"I'm only interested to use the numbers from row 14 to 514."
%Specific approach
%Directly specify the cells to get data from
mat = readmatrix('spatialcurve.xls','Range','B14:H514')
%Generalized answer
%Specify rows to get data from
mat = readmatrix('spatialcurve.xls','Range','14:514')
%and delete any NaN columns
mat(:,all(isnan(mat),1))=[]
0 Comments
Bruno Luong
on 1 Sep 2023
Edited: Bruno Luong
on 1 Sep 2023
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1471061/spatialcurve.xls')
A=T{1:end,2:end} % remove first column that contains string, adapt range to your need
class(A)
size(A)
9 Comments
Bruno Luong
on 4 Sep 2023
Edited: Bruno Luong
on 4 Sep 2023
load T.mat % R2019b : T = readtable('spatialcurve.xls')
c26=T{13:512,2:6};
c78=T{13:512,7:8};
A=[str2double(c26) c78]
See Also
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!