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!