if else statement
이전 댓글 표시
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
Jan
2012년 4월 11일
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
2012년 4월 11일
Thomas
2012년 4월 11일
@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
2012년 4월 11일
douglas
2012년 4월 11일
Thomas
2012년 4월 11일
can you give me a sample of your text file.?
douglas
2012년 4월 11일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Language Support에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!