if else statement
Mostrar comentarios más antiguos
I have a for loop that used to open text files for plotting, and I want to have an if else statement that will set headerlines equal to 5 for test files having less than 500 rows, and headerlines equal to 400 for those having in excess of 500 rows. I have this so far but it does not seem to be working;
for k = 58:212
inputFileName = sprintf('MT_%05i-000.txt',k);
outputFileName = sprintf('results%05i.tiff',k);
fid = fopen(inputFileName);
newcmd=sprintf('more %s|wc -l', inputFileName);
[p,num_lines]=system(newcmd);
if num_lines<=500
datacell = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','HeaderLines',5);
fclose(fid);
else
datacell = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','HeaderLines',500);
fclose(fid); %number of %f reflects number of columns to record from text file
end
7 comentarios
Jan
el 11 de Abr. de 2012
Please explain, what "does not seem to be working" means explicitely. It is hard to give an advice for an unknown problem, which appears in code, we cannot run. At first we do not have your data files, at second the effects of "system(newcmd)" are unknown.
douglas
el 11 de Abr. de 2012
Thomas
el 11 de Abr. de 2012
@Jan, here is the question that went before it: http://www.mathworks.com/matlabcentral/answers/35073-for-loop-question-plot-figure
I suggested the newcmd if the user was on linux to count the num_lines
new_cmd=sprintf('more %s|wc -l', inputFileName);
[p,num_lines]=system(newcmd);
douglas
el 11 de Abr. de 2012
douglas
el 11 de Abr. de 2012
Thomas
el 11 de Abr. de 2012
can you give me a sample of your text file.?
douglas
el 11 de Abr. de 2012
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Language Support en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!