MATLAB: How to plot data in block

data in block

Hello, I've data in .txt file as follows:
-42.135 -40.263 -38.384 -36.498 -34.608 -32.712 -30.811 -28.907 -26.999 -25.088
-23.174 -21.258 -19.339 -17.419 -15.497 -13.573 -11.648 -9.723 -7.796 -5.868
-3.940 -2.012 -0.084 1.845 3.773 5.701 7.629 9.555 11.481 13.406
15.330 17.252 19.173 21.092 23.008 24.922 26.833 28.742 30.646 32.547
34.443 36.335 38.220 40.100 41.972 43.837 45.692 47.537 49.371 51.191
52.995 54.781
5.543 5.325 5.061 4.786 4.535 4.328 4.146 3.988 3.864 3.779
3.728 3.705 3.715 3.763 3.862 4.044 4.375 4.985 6.032 7.594
9.759 12.214 13.494 14.008 13.810 13.130 12.751 12.998 13.764 14.557
14.307 13.415 13.135 13.130 12.965 12.590 11.975 11.391 10.867 10.446
10.088 9.760 9.401 9.021 8.594 8.124 7.615 7.106 6.737 6.495
6.292 6.119
I want to plot above upper block(52 data points) data in x-axis and that in lower block in y-axis using matlab command. Any suggestions?

Best Answer

Knowing the length of the data sections makes it pretty easy even though they're not regular in the file--
fid=fopen('yourfile.txt','r'); % open the data file to read...
x=fscanf(fid,'%f',52); % read the known number of x values
y=fscanf(fid,'%f',52); % followed by y values
fid=fclose(fid);
plot(x,y)
NB: that fscanf will
  1. Read the first N requested values (and no more) into 'x', and then
  2. skip the blanks in the file to read the 52 more values into 'y'
Since that works like magic, afterwards the plotting is trivial. If use importdata or one of the other higher-level routines, it would read all the data but then you've got a mess in trying to sort out "who's who in the zoo" as those routines will return a regular array w/ NaN for the missing values in the rows shorter than the rest.