Import files from two folders

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 Comment

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}));

Sign in to comment.

 Accepted Answer

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 Comments

thank you! Works perfectly! Did not think about what you mentioned. A short side question, do you have an idea of how I can take out the file-names from the loops given in the question? I.e. , I want to post-process them in the main file afterwards
Hi again,
I think it is easier to add the file names after the loop. However, I have some problems with the different data types. After the loop I have a table consisting of 10000x10 (double data-type) and the subfolder names as a 1*10 (cell data-type). I don't manage to add the file names as a new row to the table...I tried to convert the 10000x10 double to a table and use dot notation. However, this did not work either when I wanted to use the 1*10-file name (i.e. subfolderNames) directly in the code. I know it is possible to write the file names using 'RowNames' but how can I do this automatically with the names of the cells?
Thank you
Please post the corresponding code. Then it is easier to suggest a modification.
The code is as appended above (with modification according to you). See below.
Main script:
path2 = 'files2';
subfolderInfo = dir(path2);
subfolderNames = {subfolderInfo.name};
subfolderNames(ismember(subfolderNames,{'.','..'}))=[];
Num2=numel(subfolderNames);
for c = 1: Num2
[Z1] = Importing(c,subfolderNames);
end
And the code in the functions is:
function [Output]=Importing(v,subfolderNames)
fileInfo = dir(fullfile('files1', subfolderNames{v}));
fileNames = {fileInfo.name};
fileNames(ismember(fileNames,{'.','..'}))=[];
ImportedData = readtable(char(fullfile(fileInfo(1).folder,fileNames(1))));
Data = table2array(ImportedData(:,1));%Data-column
As an example, I'm appending two examples of the files that I want to read. Please see attached excel-files. To clarify, I want the file names (i.e. Mat.. and Mat..2) as new row names for the Data-columns(see comment in the code above) which I get from running the script (output=Z1 in the main script). Hence, according to the example above it should be two columns containing the data given in the appended excel-files with Mat_question.xlsx and Mat_question2.xlsx as the row names, respectively.
I solved it by myself, thank you Jan!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Asked:

on 7 Dec 2021

Commented:

on 8 Dec 2021

Community Treasure Hunt

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

Start Hunting!