Debugging help needed please :(

I am trying to have a loop that extracts all files that do not start with ~$ and ends in xls or xlsx
This is what I have
n = length(list);
%list is an cell array of a list of folders that I have
i = 0; ii=0;
for k = 1 : n
Sub = list{k};
filehunt = fullfile(Sub, '*.xls*');
Excel = dir(filehunt);
%here I use the loop to "dir" all files that contain the keywords .xls
if ~isempty(Excel)
for k2 = 1 : length(Excel)
ii=ii+1;
if strncmp('~$',Excel(k2).name,2)== 0
goodexcel(ii)=Excel(k2).name;
%here I try to eliminate any files that have ~$ in front
if ~isempty(goodexcel)
for k3 = 1 : length(goodexcel)
i=i+1;
if ~isempty(regexp(goodexcel(k3).name, '\.xlsx?$', 'once'));
ExcellentExcel(i)=goodexcel(k3).name
%here I try to find all files that end in xls or xlsx instead of just having .xls somewhere in the file name
end
end
end
end
end
end
end
This has not been able to work due to the error:
Subscripted assignment dimension mismatch.
Please enlighten my poor brain with any matlab coding skill/tips.
Thank you for your patience.

 Respuesta aceptada

Image Analyst
Image Analyst el 15 de Jul. de 2016
Editada: Image Analyst el 15 de Jul. de 2016

0 votos

Check this out:
if all(Excel(k2).name(1:2) == '~$')
msgbox('It starts with ~$ so skip this file');
break; % Skip to bottom of loop
else
msgbox('It does not start with ~$');
end

9 comentarios

chlor thanks
chlor thanks el 15 de Jul. de 2016
Thank you Image Analyst. This is very nice, but I do not want any msgbox, instead I just wish to execute the code to find the files.
Can I do
n = length(list);
i = 0; ii=0;
for k = 1 : n
Sub = list{k};
filehunt = fullfile(Sub, '*.xls*');
Excel = dir(filehunt);
if ~isempty(Excel)
for k2 = 1 : length(Excel)
ii=ii+1;
if Excel(k2)name(1:2) ~== '~$'
goodexcel(ii)=Excel(k2).name;
for k3 = 1 : length(goodexcel)
i=i+1;
if ~isempty(regexp(goodexcel(k3).name, '\.xlsx?$', 'once'));
ExcellentExcel(i)=goodexcel(k3).name
end
end
end
end
end
end
Image Analyst
Image Analyst el 15 de Jul. de 2016
OK, you can take out the msgbox of course, buy you cannot take out the all() like you did or it won't work right. Also there is no concept of ~== so I don't know why you made that change also. Just use the line of code I gave you, unless you don't want it to work.
I recommend
if strncmp(Excel(k2).name, '~$', 2)
chlor thanks
chlor thanks el 18 de Jul. de 2016
Editada: chlor thanks el 18 de Jul. de 2016
Sorry, I know I have been making tons of stupid mistakes as I still learning a lot of the basics.
So I tried to use this instead
n = length(list);
i = 0;
for k = 1 : n
Sub = list{k};
filehunt = fullfile(Sub, '*.xls*'); %target all the xls and xlsx
Excel = dir(filehunt);
if ~isempty(regexp(Excel(k).name, '\.xlsx?$', 'once'))
for k2 = 1 : length(Excel)
i = i + 1;
if all(Excel(k2).name(1:2) == '~$')
break;
else
ExcellentExcel(i).name =Excel(k2).name;
end
end
end
end
The result is good with removing all the files that start with ~$ but is not able to remove the files that do not end with xls or xlsx. Also, there are many empty cells within the array when I execute
ExcellentExcel.name
Can you please take a few seconds to look at my code again and tell me what is wrong with it?
Thank you for reading and I appreciate your patience with me.
chlor thanks
chlor thanks el 18 de Jul. de 2016
I will try what you recommend too, thanks Walter.
Image Analyst
Image Analyst el 18 de Jul. de 2016
This line:
filehunt = fullfile(Sub, '*.xls*')
when passed in to dir() should not return any files that have a different extension than .xls or .xlsx. What is the filename of a different file that you say it's returning?
chlor thanks
chlor thanks el 19 de Jul. de 2016
Editada: chlor thanks el 19 de Jul. de 2016
It returns something like interesting.xls.pptm which is a powerpoint but has the xls extension in it as well...
Image Analyst
Image Analyst el 19 de Jul. de 2016
You can use fileparts() to pull off the last extension
[folder, baseFileNameWithoutExtension, extension] = fileparts(filename);
Then just use extension to see whether to add to the final list.
chlor thanks
chlor thanks el 19 de Jul. de 2016
Although I still have not able to figure out how to get rid of the empty cells returned by the loop. The fileparts() command works great and better than the regexp() I was trying with before. Thank you very much for the helpful info!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Jul. de 2016

Comentada:

el 19 de Jul. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by