How to transform a table to a matrix?

45 visualizzazioni (ultimi 30 giorni)
Tomas
Tomas il 1 Set 2023
Modificato: dpb il 4 Set 2023
I have an excel file that I import to matlab by:
data = 'spatialcurve.xls';
flux = readtable(data,'PreserveVariableNames',true);
flux = table2array(flux);
But at the last line I get "Error using table2array (line 37)
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);
Then I get "Error using cell2mat (line 45)
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 Commenti
Dyuman Joshi
Dyuman Joshi il 1 Set 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
Tomas il 1 Set 2023
I'm only interested to use the numbers from row 14 to 514.

Accedi per commentare.

Risposta accettata

dpb
dpb il 1 Set 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 Commenti
Tomas
Tomas il 4 Set 2023
I use matlab21019b. Don´t know if this is a suffuciently new version to be able to do the things you do?
dpb
dpb il 4 Set 2023
Modificato: dpb il 4 Set 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.

Accedi per commentare.

Più risposte (2)

Dyuman Joshi
Dyuman Joshi il 1 Set 2023
Modificato: Dyuman Joshi il 1 Set 2023
"I'm only interested to use the numbers from row 14 to 514."
You can utilize the range functionality of readmatrix -
%Specific approach
%Directly specify the cells to get data from
mat = readmatrix('spatialcurve.xls','Range','B14:H514')
mat = 501×7
0 -5.0000 0 3.0000 -132.3684 -0.0463 -20.6779 0.0200 -4.9800 0 3.0000 -133.8162 0.0142 -20.3929 0.0400 -4.9600 0 3.0000 -135.2797 0.0227 -20.0503 0.0600 -4.9400 0 3.0000 -136.7431 0.0298 -19.7097 0.0800 -4.9200 0 3.0000 -138.2065 0.0355 -19.3712 0.1000 -4.9000 0 3.0000 -139.6699 0.0397 -19.0346 0.1200 -4.8800 0 3.0000 -141.2159 0.0402 -18.7169 0.1400 -4.8600 0 3.0000 -142.7639 0.0405 -18.3387 0.1600 -4.8400 0 3.0000 -144.3119 0.0404 -17.9608 0.1800 -4.8200 0 3.0000 -145.8599 0.0401 -17.5830
%Generalized answer
%Specify rows to get data from
mat = readmatrix('spatialcurve.xls','Range','14:514')
mat = 501×8
NaN 0 -5.0000 0 3.0000 -132.3684 -0.0463 -20.6779 NaN 0.0200 -4.9800 0 3.0000 -133.8162 0.0142 -20.3929 NaN 0.0400 -4.9600 0 3.0000 -135.2797 0.0227 -20.0503 NaN 0.0600 -4.9400 0 3.0000 -136.7431 0.0298 -19.7097 NaN 0.0800 -4.9200 0 3.0000 -138.2065 0.0355 -19.3712 NaN 0.1000 -4.9000 0 3.0000 -139.6699 0.0397 -19.0346 NaN 0.1200 -4.8800 0 3.0000 -141.2159 0.0402 -18.7169 NaN 0.1400 -4.8600 0 3.0000 -142.7639 0.0405 -18.3387 NaN 0.1600 -4.8400 0 3.0000 -144.3119 0.0404 -17.9608 NaN 0.1800 -4.8200 0 3.0000 -145.8599 0.0401 -17.5830
%and delete any NaN columns
mat(:,all(isnan(mat),1))=[]
mat = 501×7
0 -5.0000 0 3.0000 -132.3684 -0.0463 -20.6779 0.0200 -4.9800 0 3.0000 -133.8162 0.0142 -20.3929 0.0400 -4.9600 0 3.0000 -135.2797 0.0227 -20.0503 0.0600 -4.9400 0 3.0000 -136.7431 0.0298 -19.7097 0.0800 -4.9200 0 3.0000 -138.2065 0.0355 -19.3712 0.1000 -4.9000 0 3.0000 -139.6699 0.0397 -19.0346 0.1200 -4.8800 0 3.0000 -141.2159 0.0402 -18.7169 0.1400 -4.8600 0 3.0000 -142.7639 0.0405 -18.3387 0.1600 -4.8400 0 3.0000 -144.3119 0.0404 -17.9608 0.1800 -4.8200 0 3.0000 -145.8599 0.0401 -17.5830

Bruno Luong
Bruno Luong il 1 Set 2023
Modificato: Bruno Luong il 1 Set 2023
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1471061/spatialcurve.xls')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 516×8 table
Var1 Support GlobalCoordinates Var4 Var5 C1 Var7 Var8 __________ _______ _________________ ____ ____ _______ _________ _______ {'Labels'} NaN NaN NaN NaN NaN NaN NaN {'Units' } NaN NaN NaN NaN NaN NaN NaN {'Values'} 0 -5 0 3 -132.37 -0.046264 -20.678 {0×0 char} 0.02 -4.98 0 3 -133.82 0.014182 -20.393 {0×0 char} 0.04 -4.96 0 3 -135.28 0.022695 -20.05 {0×0 char} 0.06 -4.94 0 3 -136.74 0.029787 -19.71 {0×0 char} 0.08 -4.92 0 3 -138.21 0.035458 -19.371 {0×0 char} 0.1 -4.9 0 3 -139.67 0.039708 -19.035 {0×0 char} 0.12 -4.88 0 3 -141.22 0.04023 -18.717 {0×0 char} 0.14 -4.86 0 3 -142.76 0.040461 -18.339 {0×0 char} 0.16 -4.84 0 3 -144.31 0.040402 -17.961 {0×0 char} 0.18 -4.82 0 3 -145.86 0.040053 -17.583 {0×0 char} 0.2 -4.8 0 3 -147.41 0.039413 -17.205 {0×0 char} 0.22 -4.78 0 3 -149.05 0.033003 -16.828 {0×0 char} 0.24 -4.76 0 3 -150.69 0.037098 -16.383 {0×0 char} 0.26 -4.74 0 3 -152.33 0.041193 -15.938
A=T{1:end,2:end} % remove first column that contains string, adapt range to your need
A = 516×7
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 -5.0000 0 3.0000 -132.3684 -0.0463 -20.6779 0.0200 -4.9800 0 3.0000 -133.8162 0.0142 -20.3929 0.0400 -4.9600 0 3.0000 -135.2797 0.0227 -20.0503 0.0600 -4.9400 0 3.0000 -136.7431 0.0298 -19.7097 0.0800 -4.9200 0 3.0000 -138.2065 0.0355 -19.3712 0.1000 -4.9000 0 3.0000 -139.6699 0.0397 -19.0346 0.1200 -4.8800 0 3.0000 -141.2159 0.0402 -18.7169 0.1400 -4.8600 0 3.0000 -142.7639 0.0405 -18.3387
class(A)
ans = 'double'
size(A)
ans = 1×2
516 7
  9 Commenti
Tomas
Tomas il 4 Set 2023
OK, so it seems that it's something that is not working as supposed to with my readtable, hmm.. But I have attched the table as .mat.
Bruno Luong
Bruno Luong il 4 Set 2023
Modificato: Bruno Luong il 4 Set 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]
A = 500×7
0.0200 -4.9800 0 3.0000 -133.8162 0.0142 -20.3929 0.0400 -4.9600 0 3.0000 -135.2797 0.0227 -20.0503 0.0600 -4.9400 0 3.0000 -136.7431 0.0298 -19.7097 0.0800 -4.9200 0 3.0000 -138.2065 0.0355 -19.3712 0.1000 -4.9000 0 3.0000 -139.6699 0.0397 -19.0346 0.1200 -4.8800 0 3.0000 -141.2159 0.0402 -18.7169 0.1400 -4.8600 0 3.0000 -142.7639 0.0405 -18.3387 0.1600 -4.8400 0 3.0000 -144.3119 0.0404 -17.9608 0.1800 -4.8200 0 3.0000 -145.8599 0.0401 -17.5830 0.2000 -4.8000 0 3.0000 -147.4079 0.0394 -17.2054

Accedi per commentare.

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by