How to transform a table to a matrix?
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.
Tomas
on 1 Sep 2023
Accepted Answer
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))=[]
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
Tomas
on 4 Sep 2023
Bruno Luong
on 4 Sep 2023
My code run with your data you have provided.
If you have different data then you should check if the data are similar, or restricted to the rectangular array where only double class is presented, or you have to provide the data if you want to us to take a look.
Tomas
on 4 Sep 2023
Bruno Luong
on 4 Sep 2023
Edited: Bruno Luong
on 4 Sep 2023
Could be I don't have R2019b to test. In the code
I wrote the comment "adapt range to your need". Did you attemp to do that?
Tomas
on 4 Sep 2023
Tomas
on 4 Sep 2023
Bruno Luong
on 4 Sep 2023
Can you save T returned by readtable in mat file and attach here?
But with visual inspection it looks like the reatable in your version is not able to read data from this xls file. Many field contain just 0x0 char.
Tomas
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]
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!