How to transform a table to a matrix?
古いコメントを表示
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 件のコメント
Dyuman Joshi
2023 年 9 月 1 日
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
2023 年 9 月 1 日
採用された回答
その他の回答 (2 件)
Dyuman Joshi
2023 年 9 月 1 日
編集済み: Dyuman Joshi
2023 年 9 月 1 日
"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))=[]
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 件のコメント
Tomas
2023 年 9 月 4 日
Bruno Luong
2023 年 9 月 4 日
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
2023 年 9 月 4 日
Bruno Luong
2023 年 9 月 4 日
編集済み: Bruno Luong
2023 年 9 月 4 日
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
2023 年 9 月 4 日
Tomas
2023 年 9 月 4 日
Bruno Luong
2023 年 9 月 4 日
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
2023 年 9 月 4 日
load T.mat % R2019b : T = readtable('spatialcurve.xls')
c26=T{13:512,2:6};
c78=T{13:512,7:8};
A=[str2double(c26) c78]
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!