Using readtable to load excel files in a different folder

9 visualizzazioni (ultimi 30 giorni)
I used dir to get the files names in a specified folder "Formatted_Excel" that exists within the working folder and that works just fine. But I need to use load the data from those files so I try using the readtable function, but MATLAB does not recognize that they exist. Does anyone know how to get around this?
The one tricky part is that sometimes MATLAB does recognize that they exist and it loads them, but it loads them from the earlier folder named "Excel" that also exists within the working folder. The folders both contain the same number and named files but the "Formatted_Excel" folder contains files that are all formatted the same thus making them easy to manipulate in matlab. Please someone help!
Heres my code:
%getting all of the data from folder 'Formatted_Excel' containing files for each participant
folder_read_in =dir('Formatted_Excel');
% getting names of all relevent files (last command is bc matlab thinks
% there are extra files in that folder for some reason)
files = {folder_read_in.name}; files = string(files(4:end));
P = readtable(files(1));
Thanks!

Risposta accettata

dpb
dpb il 25 Lug 2019
folder_in='Formatted_Excel'; % directory of interest
d=dir(fullfile(folder_in,'*.xls*'); % return the .xls files in the given folder
for i=1:numel(d)
P=readtable(fullfile(folder_in,d(i).name);
...
% do whatever w/ the i-th file here before going on to the next...
...
end
  2 Commenti
dpb
dpb il 20 Feb 2022
Modificato: dpb il 20 Feb 2022
Not in some form or fashion, no. readtable is not vectorized internally to handle more than one file at a time. Nor are any other of the MATLAB file i/o functions.
There is (or at least used to be, I presume it is still available there) a File Exchange m-file function FILEFUNCTION that provides a similar functionality for multiple files as do arrayfun or cellfun for numeric or cell arrays that provides a higher level of abstraction by moving the loop to a lower level, but it still consists of the for...end loop therein. Of course, the builtin functions are doing the same internally as well.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by