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

4 visualizaciones (últimos 30 días)
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 comentarios
KSSV
KSSV el 9 de Jun. de 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 el 9 de Jun. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 9 de Jun. de 2017
Editada: Stephen23 el 9 de Jun. de 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 comentarios
Stephen23
Stephen23 el 9 de Jun. de 2017
Editada: Stephen23 el 9 de Jun. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by