Import files from two folders
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi,
I am trying to import files from two different folders, including each file name. I manage to import the files from the first folder but I have problems when I come to the second folder.I get the error: "Undefined function 'Importing' for input arguments of type 'cell'.My code looks as follows:
path1 = 'files1';
subfolderInfo = dir(path1);
subfolderNames = {subfolderInfo.name};
subfolderNames(ismember(subfolderNames,{'.','..'}))=[];
Num1=numel(subfolderNames);
for b = 1: Num1
[X1,X2] = PImport(b,subfolderNames);
end
%in this folder I get the problem...
path2 = 'files2';
subfolderInfo = dir(path2);
subfolderNames = {subfolderInfo.name};
subfolderNames(ismember(subfolderNames,{'.','..'}))=[];
Num2=numel(subfolderNames);
for c = 1: Num2
[Z1] = ImportingEl(c,subfolderNames);
end
I don't know if it the connected to the use of "subfolderNames" twice. However, I have tried changing it to "SubfolderNames2" but that does not work either. Furthermore, the code within each for loop is as follows:
cd 'files1';
fileInfo = dir(subfolderNames{v});
fileNames = {fileInfo.name};
fileNames(ismember(fileNames,{'.','..'}))=[];
ImportedData = readtable(char(fullfile(fileInfo(1).folder,fileNames(1))));
Data = table2array(ImportedData(:,2));
Data(1:4,:)=[];
Thank you in advance!
댓글 수: 1
Jan
2021년 12월 7일
A simplification: If you access a cell string with curly braces, you can omit the conversion by CHAR():
% ImportedData = readtable(char(fullfile(fileInfo(1).folder,fileNames(1))));
ImportedData = readtable(fullfile(fileInfo(1).folder, fileNames{1}));
채택된 답변
Jan
2021년 12월 7일
The error message is clear:
Undefined function 'Importing'
This means, that this function is not visible. If it was working before, I guess it is stored in a folder, which was the current folder, but then the current folder was changed by a cd() command.
Store functions in folders, which are included in Matlab's path. See:
doc addpath
doc savepath
doc pathtool
Do not change the current directory. Remember than callbacks of timers of GUIs might call CD also at unexpected situations. Prefer working with absolute paths in every case. Replace
cd 'files1';
fileInfo = dir(subfolderNames{v});
by
fileInfo = dir(fullfile('files1', subfolderNames{v}));
But even here 'files1' is a relative path. Better: 'D:\Your\Folder\files1'.
댓글 수: 5
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!