Effacer les filtres
Effacer les filtres

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

7 vues (au cours des 30 derniers jours)
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 commentaires
KSSV
KSSV le 9 Juin 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 le 9 Juin 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

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 9 Juin 2017
Modifié(e) : Stephen23 le 9 Juin 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 commentaires
Stephen23
Stephen23 le 9 Juin 2017
Modifié(e) : Stephen23 le 9 Juin 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by