Borrar filtros
Borrar filtros

Problem using the results of the ls fxn

2 visualizaciones (últimos 30 días)
Karl
Karl el 19 de Jul. de 2011
Hey All,
I'be been using the ls fxn to make my life easier. I've been expanding a script that I've been working on for a few days now (note recent question history :S ) and I've run into a small problem.
My original code:
filelist = ls('*.csv');
for j=1:size(filelist,1)
if strcmp(filename,strtrim(filelist(j,1:end)))
rowtoremove=j;
end
end
filelist(rowtoremove,:)=[];
So whats going on here? I'm trying to remove the original file I was working on that was in the same folder as a number of others from a list of those other files. Now this seems to work perfectly and I've been working with it for a bout a day now.
The problem came when I needed to exclude a series of files as well as the original. I originally tried something of this sort...
filelist = ls('*.csv');
for j=1:size(filelist,1)
if strcmp(filename,strtrim(filelist(j,1:end)))
rowtoremove=j;
end
if strfind(filelist(j,1:end),'long')
filelist(j,:)=[];
end
end
filelist(rowtoremove,:)=[];
As you might imagine this creates a large issue as I remove rows before they are evaluated and lowers the number of rows in the array/matrix so that the j counter overshoots the number of rows left.
I'm apparently a bit fatigued atm and not being my usual clever self. I originally thought I could fix this issue by counting backwards but it doesn't seem that matlab allows for that. Any ideas?
Thanks for your time! Karl

Respuesta aceptada

Karl
Karl el 20 de Jul. de 2011
So two things I discovered about Matlab while working on this loop.
A) Matlab does not let you modify the index (counter) of a for loop inside of the loop. This should be fixed.
B) Matlab will not allow a for loop to count back/down. This should also be fixed.
The solution, use a while loop and ignore Matlab's crippled implementation of for.
The final code I ended up using:
filelist = ls('*.csv');
j=size(filelist,1);
while j>0
if strcmp(filename,strtrim(filelist(j,1:end)))
filelist(j,:)=[];
j=j-1;
end
if strfind(filelist(j,1:end),'long')
filelist(j,:)=[];
end
j=j-1;
end
Thanks for the help again Walter. Yesterday I just wasn't running on all cylinders apparently.
Karl
  2 comentarios
Sean de Wolski
Sean de Wolski el 20 de Jul. de 2011
The first count is an opinion. I disagree with you, MATLAB should not change the way the for-loop works - that's specifically why while-loops exist.
Your second count is plain WRONG. I showed below how a for-loop runs backward. What happens if you run this:
for ii = 10:-1:1
disp(ii)
end
You just have to tell ML the increment for the for-loop. It could also be fractional or more than 1.
for ii = 10:-2:0
disp(ii)
end
or
for ii = 0:.25:2
disp(ii)
end
Walter Roberson
Walter Roberson el 20 de Jul. de 2011
The knowledge that the loop iterations cannot (permanently) change the loop variable is important for optimization, especially when any kind of parallel processing is involved.
There are a number of computer languages that treat the loop variable as being a constant within the loop, with it being a syntax error to attempt to modify the loop variable. There are other computer languages for which the loop variable's scope is restricted to the loop and it is an error to try to refer to the value of the loop variable after the loop terminates. MATLAB's treatment of for loop variables might not be what you are accustomed to, but it is not "wrong" and does not need to be changed. MATLAB's usage is, indeed, consistent with Algol 68.
MATLAB's behavior of not counting backwards if the start value is greater than the stop value is a behavior shared with numerous computer languages, including Fortran, PASCAL, Algol 68; likewise, switching the limits of a for loop does not alter the direction of counting in C, AWK, COBOL, or Perl.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 19 de Jul. de 2011
If you are removing rows by deleting them, then it is usually a good idea to work backwards, so that no row becomes movable until after you have finished with it.
  2 comentarios
Karl
Karl el 19 de Jul. de 2011
Yes, but how do I do that as matlab doesn't seem to like that... unless I guess everywhere I want to use j I could use j-#... that might work... :O
Sean de Wolski
Sean de Wolski el 19 de Jul. de 2011
for j = maxj:-1:1
%do stuff
end
to run it backwards.

Iniciar sesión para comentar.

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by