Function with two input arguments to do the following actions?? - Homework

This function will have two inputs, a path of a directory file and a file extension.
For example: 'C:\lime\green' and 'pdf'
The function will need to :
1. Find the total number of files with the given file extension
2. Find the name and size of the largest file with the given extension
3. name and size of the smallest files with the given extension in this folder
4. Return the names of all files with this extension that are in the folder.
What I have is still a work in progress:
function FolderInfo( path,fext )
D = dir(path);
% 18 1
[~, c] = size(D);
cell = struct2cell(D);
cn = 0;
for i = c
s1 = cell(1,i);
if s1(end-3) == fext
cn = cn + 1;
cs{cn}=str;
end
end
fprintf('THERE ARE A TOTAL OF %.1f FILES IN FOLDER.',length(D));
fprintf('%s WITH EXTENSION %s',path,fext);
celldisp(cs);
end
Please comment on why you do the changes you do. Thank you.

 Respuesta aceptada

3 comentarios

function FolderInfo( path,fext )
D = dir(path);
cell = struct2cell(D);
[~, c] = size(cell);
cn = 0;
for i = 1:c
s = cell{1,i};
if strcmp(s(end-2:end),fext) == 1
cn = cn + 1;
cs{cn}=str;
end
celldisp(cs);
end
end
I get this error:
EDU>> FolderInfo('C:\Users\Nora.Mav\Documents\MATLAB\HW\HW11','txt')
Subscript indices must either be real positive integers or logicals.
Error in FolderInfo (line 12)
if strcmp(s(end-2:end),fext) == 1
What is it in my script that is doing this error?
You have entries that are shorter 2 characters long or shorter. Every directory has an entry named "." and another named ".."
For #2 and #3 I am having a hard time to find those names, but i can output the number of which is higher. This is what I have.
function FolderInfo( path,fext )
D = dir(path);
cell = struct2cell(D);
cell(:,1) = [];
cell(:,1) = [];
[~, c] = size(cell);
cn = 0;
for i = 1:c
s = cell{1,i};
if strcmp(s(end-2:end),fext) == 1
cn = cn + 1;
fprintf('THERE ARE A TOTAL OF %d FILE(S) IN FOLDER ',cn);
fprintf('''%s'' WITH EXTENSION ''%s''. \n',path,fext);
end
end
C = cell(3,:);
m = cell2mat(C);
M = max(m);
fprintf('THE LARGEST FILE IS WITH %d BYTES. \n',M);
C = cell(3,:);
m = cell2mat(C);
M = min(m);
fprintf('THE SMALLEST FILE IS WITH %d BYTES. \n',M);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Search Path en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Nov. de 2013

Comentada:

el 10 de Nov. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by