Adding extension to fille

조회 수: 3 (최근 30일)
Mateusz Brzezinski
Mateusz Brzezinski 2021년 11월 9일
댓글: Image Analyst 2021년 11월 10일
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:
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
Geoff Hayes
Geoff Hayes 2021년 11월 9일
@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.
Mateusz Brzezinski
Mateusz Brzezinski 2021년 11월 9일
Hello, thanks for answer.
Error is:
Error using load
Unable to read file
'C:\Users\brzezinskim_admin\OneDrive\06
[Education]\Projects\SPP2019\Programs\MathLab\CARS
Importer\Data\.'. Input cannot be a directory.
Error in Cars_Import_singles_Save_map (line 23)
Strings{iFile} = load(File);
for all of:
FileList = dir(fullfile(Folder, '**', '*.*'));
%or
FileList = dir(fullfile(Folder, '**', '*.data.1'));
%or
FileList = dir(Folder)
%or
FileList = dir(fullfile(Folder, '**'));
%or
FileList = dir(fullfile(Folder));
I have only those filles (without extension), nothing else.

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

채택된 답변

Stephen23
Stephen23 2021년 11월 9일
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
Mateusz Brzezinski
Mateusz Brzezinski 2021년 11월 10일
I modified it to:
P = 'Data';
S = dir(fullfile(P,'**','*'));
S = S(~[S.isdir]);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
Z(k).data = readmatrix(F);
end
And now it's fine
Stephen23
Stephen23 2021년 11월 10일
"I modified it to:"
Why do you need to create a second structure Z, when S already exists?

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 11월 9일
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
Mateusz Brzezinski
Mateusz Brzezinski 2021년 11월 9일
It's working but now I have sth like that - 2 first cells are empty. Can I get rid of anyhow in the creation process ?
Image Analyst
Image Analyst 2021년 11월 10일
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.

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

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by