简体   繁体   中英

Import multiple tables in MATLAB

I need to import many tables (.xlsx format) in MATLAB. I hence tried to do so with a for loop.

The code is reported below:

addpath('C:\...'); % here hide the actual path just for simplicity
numfiles = 50;
mydata = cell(1, numfiles);
for k = 1:numfiles
  myfilename = sprintf('Table_%d.xlsx', k);
  mydata{k} = importdata(myfilename);
end

Even if this actually works it raises problems as duration (hh:mm:ss) and UTCdate arrays are either converted in simple double or NaN.

Can anyone suggest a way around this that solve my problem?

You can likely use the readtable functionality available in the newer versions of Matlab. This creates a matlab table object which should work for what you need.

The option ('DatetimeType','Text') should help you address your time problem (by writing custom code to handle your date format).

In your case I'd rather use the xlsread function. It specifically targets Excel files, hence saving some time compared to readtable or importdata . You can use it in the following way:

Imagine you create a .xls file that looks like this:

First     Second    Third
    1          2        3
    4          5     x    
01:43:00       8        9

Reading those files with xlsread will output three matrices in total.

[num,txt,raw] = xlsread('myExample.xlsx')

num =
     1     2     3
     4     5   NaN
   NaN     8     9

txt = 
   'First'    'Second'    'Third'
   ''         ''          ''     
   ''         ''          'x'    
   '01:43:00' ''          ''    

raw = 
    'First'    'Second'    'Third'
    [    1]    [     2]    [    3]
    [    4]    [     5]    'x'    
   '01:43:00'  [     8]    [    9]

You can customize the import in many different ways, applying filters, ignoring certain data structures etc. I recommend you going through the Matlab documentation for xlsread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM