How do i print file names that exist in certain folder in matlab table with out file extension?

3 views (last 30 days)
Dear all..
i'm using the following code to write file names that exist in folder or sub-folder ... in matlab table.. but its write these files along with thier extension .. such that if file AA is text file.. then its name in table is AA.txt and so on.. how do i avoid extension in my code.. thanks
projectdir = 'D:\test';
d = dir(fullfile(projectdir, '*'));
files = [];
for subdir = d([d.isdir] & ~ismember({d.name},{'.', '..'}))'
subfiles = dir(fullfile(projectdir, subdir.name, '*.txt'));
[subfiles.folder] = deal(subdir.name);
files = [files; subfiles];
end
details = struct2table(files);
  2 Comments
KSSV
KSSV on 9 Jun 2017
First place does this code work? I don't think so..you have initialed
dir = D:\test;
YOu have used inbuilt function dir as a variable, this will throw error in second line itself...
ahmed obaid
ahmed obaid on 9 Jun 2017
Dear KSSV; test is a folder include large number of sub-folders... every sub-folder involve some text files.. i'm able to write their names but perhaps without extension ...
here i'm writing html pages ... but perhaps i can written their names with out extension

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 Jun 2017
Edited: Stephen23 on 9 Jun 2017
Use fileparts to get the filename without the extensions. Something like this, where C is a cell array of those filenames:
[P,N,E] = cellfun(@fileparts,C,'uni',0)
  5 Comments
Stephen23
Stephen23 on 9 Jun 2017
Edited: Stephen23 on 9 Jun 2017
Running this (note that I changed the directory for my test):
projectdir = '.';%'D:\test';
D = dir(fullfile(projectdir, '*'));
files = [];
for subdir = D([D.isdir] & ~ismember({D.name},{'.', '..'}))'
subfiles = dir(fullfile(projectdir, subdir.name, '*.txt'));
[subfiles.folder] = deal(subdir.name);
files = [files; subfiles];
end
[~,N] = cellfun(@fileparts,{files.name},'uni',0);
[files.name_no_ext] = deal(N{:});
gives this output:
>> files.name_no_ext
ans = test1
ans = test2
ans = file3

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!