Adding extension to fille
1 view (last 30 days)
Show older comments
Mateusz Brzezinski
on 9 Nov 2021
Commented: Image Analyst
on 10 Nov 2021
I wrote such code that imports multiple files:
Folder = 'Data';
FileList = dir(fullfile(Folder, '**', '*.txt'));
Strings = cell(1, numel(FileList));
for iFile = 1:numel(FileList)
File = fullfile(FileList(iFile).folder, FileList(iFile).name);
Strings{iFile} = load(File);
end
In Data folder I have files like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/795329/image.png)
The problem is they don't have any extension (simple Ascii format of numbers that can be easily open with notepad).
Is there any option to import "any" file type or files without extension?
I have tried this, but it gives me an error.
FileList = dir(fullfile(Folder, '**', '*.*'));
%or
FileList = dir(fullfile(Folder, '**', '*.data.1'));
Any suggestion? I would appreciate any help
2 Comments
Geoff Hayes
on 9 Nov 2021
@Mateusz Brzezinski - what is the error message when you try to use either of the last two lines of code? Does your Data folder have folders or just files? If just files, then you may be able to use
FileList = dir(Folder)
to get a list of all files in that folder.
Accepted Answer
Stephen23
on 9 Nov 2021
Simpler:
P = 'Data';
S = dir(fullfile(P,'**','*'));
S = S(~[S.isdir]);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readmatrix(F);
end
2 Comments
Stephen23
on 10 Nov 2021
"I modified it to:"
Why do you need to create a second structure Z, when S already exists?
More Answers (1)
Image Analyst
on 9 Nov 2021
In your loop over files, check the isdir property to skip directories
for iFile = 1:numel(FileList)
if FileList(iFile).isdir
continue; % Skip files if they are a directory.
end
fullFileName = fullfile(FileList(iFile).folder, FileList(iFile).name);
Strings{iFile} = load(fullFileName);
end
2 Comments
Image Analyst
on 10 Nov 2021
OK, you didn't tell me you were going to load it into another cell array using the same index. If you had, I would have said to remove them like Stephen did, or else you can have a second counter index in the loop.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!