if else statement
Mostra commenti meno recenti
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 Commenti
Jan
il 11 Apr 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
il 11 Apr 2012
Thomas
il 11 Apr 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
il 11 Apr 2012
douglas
il 11 Apr 2012
Thomas
il 11 Apr 2012
can you give me a sample of your text file.?
douglas
il 11 Apr 2012
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Language Support in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!