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

7 次查看(过去 30 天)
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 个评论
KSSV
KSSV 2017-6-9
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 2017-6-9
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

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2017-6-9
编辑:Stephen23 2017-6-9
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 个评论
Stephen23
Stephen23 2017-6-9
编辑:Stephen23 2017-6-9
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by