if else statement

1 view (last 30 days)
douglas
douglas on 11 Apr 2012
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
Thomas
Thomas on 11 Apr 2012
can you give me a sample of your text file.?
douglas
douglas on 11 Apr 2012
// Start Time: 0
// Sample rate: 10.0Hz
// Scenario: 1.8
// Firmware Version: 2.5.1
Year Month Day Second Temperature Acc_X Acc_Y Acc_Z Gyr_X Gyr_Y Gyr_Z Mag_X Mag_Y Mag_Z Roll Pitch Yaw AnalogIn_1 AnalogIn_2 Latitude Longitude Altitude Vel_X Vel_Y Vel_Z Status
0 0 0 0.0083 17.13 -3.602011 -0.023927 9.130322 0.002424 -0.003330 0.005214 0.551597 0.036565 -0.615183 -0.150150 21.529704 -6.936187 0 0 0.000000000 0.000000000 0.000000 0.000000 0.000000 0.000000 1
0 0 0 0.1083 17.13 -3.616401 -0.028699 9.101781 -0.013352 0.002609 -0.002676 0.546472 0.050605 -0.614217 -0.232993 21.544571 -6.953156 0 0 -0.000000001 0.000000000 -0.000094 -0.002426 -0.000193 -0.001876 1
0 0 0 0.2083 17.13 -3.626294 -0.006795 9.111114 0.005820 0.006014 0.006351 0.548270 0.048896 -0.615307 -0.185587 21.579128 -6.914592 0 0 -0.000000004 -0.000000001 -0.000314 -0.004752 0.003139 -0.002529 1
0 0 0 0.3083 17.13 -3.599662 -0.019086 9.139808 -0.003034 -0.001575 -0.005997 0.548736 0.048293 -0.615885 -0.216817 21.569953 -6.951937 0 0 -0.000000008 -0.000000004 -0.000515 -0.003222 0.004013 -0.001485 1
0 0 0 0.4083 17.13 -3.601782 -0.028687 9.101578 -0.001878 0.001172 -0.003753 0.542780 0.044977 -0.605897 -0.236359 21.576554 -6.975514 0 0 -0.000000011 -0.000000008 -0.000785 -0.003494 0.004624 -0.003916 1
0 0 0 0.5083 17.13 -3.592106 -0.028719 9.111055 -0.001411 0.002545 -0.002635 0.548381 0.045602 -0.610085 -0.250711 21.591040 -6.992247 0 0 -0.000000014 -0.000000013 -0.001272 -0.002378 0.005380 -0.005822 1
0 0 0 0.6083 17.13 -3.592194 -0.023880 9.120574 -0.012431 0.002142 -0.002905 0.545978 0.050608 -0.614250 -0.328819 21.603194 -7.010641 0 0 -0.000000015 -0.000000018 -0.001905 -0.000590 0.006776 -0.006842 1
0 0 0 0.7083 17.13 -3.631372 -0.019131 9.145054 -0.000406 0.001911 0.002302 0.547899 0.049894 -0.615344 -0.326204 21.614191 -6.996948 0 0 -0.000000016 -0.000000026 -0.002455 -0.001107 0.010194 -0.004149 1
0 0 0 0.8083 17.13 -3.601852 -0.028727 9.111190

Sign in to comment.

Accepted Answer

Thomas
Thomas on 11 Apr 2012
You could try
for k = 58:212
inputFileName = sprintf('MT_%05i-000.txt',k);
outputFileName = sprintf('results%05i.tiff',k);
fid = fopen(inputFileName);
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');
if length(datacell)<=500 % this depends on the structure of datacell
actual_data=datacell(5:end) % removing only the 5 headerlines
fclose(fid);
else
actual_data=datacell(401:end) % removing 400 headerlines data > 500 rows
fclose(fid);
end
end
use only the actual_data for computation
EDIT: Try the following
for k = 58:212
inputFileName = sprintf('MT_%05i-000.txt',k);
outputFileName = sprintf('results%05i.tiff',k);
fid = fopen(inputFileName);
c=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,'delimiter',' ');
if length(c{1,1})<500 % this tell us there are less than 500 elements
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,'delimiter',' ');
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',400,'delimiter',' ');
fclose(fid);
end
end
  9 Comments
douglas
douglas on 11 Apr 2012
It seems to have fixed the problem!
Thank you so much for you help, you have been tremendous in assisting me!
Thomas
Thomas on 11 Apr 2012
You are welcome.. Please accept the other answer as well since, it might help someone on a linux system..

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export 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!