Using readtable to load excel files in a different folder

조회 수: 9 (최근 30일)
Noah Leavitt
Noah Leavitt 2019년 7월 25일
편집: dpb 2022년 2월 20일
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!

채택된 답변

dpb
dpb 2019년 7월 25일
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
Yonatan Wiegner
Yonatan Wiegner 2022년 2월 20일
Is it a way to that without the 'for' loop?
dpb
dpb 2022년 2월 20일
편집: dpb 2022년 2월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by