if else statement
Show older comments
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 Comments
Jan
on 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
on 11 Apr 2012
Thomas
on 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
on 11 Apr 2012
douglas
on 11 Apr 2012
Thomas
on 11 Apr 2012
can you give me a sample of your text file.?
douglas
on 11 Apr 2012
Accepted Answer
More Answers (0)
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!