-2

I can make a matrix x and y from data.dat which consists of 2 columns using this code:

load data.dat
x=data(:,1);
y=data(:,2);

What if I want to make matrices x1, x2, ... xn and the same with y, where x1, x2, are based on the separation by a blank line in the file data.dat:

54.510  1.420
55.294  1.819
55.859  1.935
55.999  2.381

9.017   1.600
9.518   1.916
9.868   2.217
9.896   2.368
10.113  2.533
10.424  2.552

....    ...

Based on this data example, I expect

x1=[54.510;55.294;55.859;55.999]
x2=[9.017;9.518;9.868;9.896;10.113;10.424]
y1=[1.420;1.819;1.935;2.381]
y2=[1.600;1.916;2.217;2.368;2.533;2.552]
Schorsch
  • 7,761
  • 6
  • 39
  • 65
dickyaz
  • 3
  • 2
  • What is the actual question? `load data.txt` should give you two columns of data even if you have the blank line in there. It should ignore the blank line. Just read it in as such and then do a `reshape`. If that doesn't do what you want then you need to describe your data file a little better (where are the y's?) – brechmos Jun 23 '14 at 14:31
  • You need to clarify your data.dat file a little more. what are the two columns above? x and y? Then is it x1 y1 for the first part and then x2 y2 for the second part? – brechmos Jun 23 '14 at 14:49
  • the first column from 'data.dat' is the 'x' and the second column is the 'y' what I mean is I want to make matrix 'x1', 'x2', and so on. which is the data is separate by the blank line. based on example data before, I expect 'x1=[54.510;55.294;55.859;55.999' 'x2=[9.017;9.518;9.868;9.896;10.113;10.424' – dickyaz Jun 23 '14 at 15:07

1 Answers1

0

You can use fgets to read the data from your file. It is assumed that your file is called test.dat:
(see the comments for explanations on the functionality)

% introduce a counter for all the 'blocks in the file'
block = 1;
% initialize empty vectors for x and y
x = [];
y = [];

% open the file (in this case 'test.dat')
file = fopen('test.dat');

% read the first line of the file
tline = fgets(file);

% continue as long as there are lines in the file
while ischar(tline)

    % fgets returs a string, convert to float to process:
    q = str2num(tline);

    % if q is empty, we have a blank line, otherwise, append the values to 
    % x and y

    if ~isempty(q)
        x(end+1) = q(1);
        y(end+1) = q(2);
    else

    % if there is a blank line, write x and y to a cell X and Y
    % you need a cell here, because the number of elements per block may 
    % differ

        X{block} = x;
        Y{block} = y;

    % advance the block counter

        block = block + 1;

    % clear and re-initialize x and y - this is necessary because the next
    % block may contain less values. you do not want artifacts.

        clear x y
        x = [];
        y = [];
    end

    % read the next line

    tline = fgets(file);

    % at the end of the file, fgets returns not a string but a number, -1
    % in this case, assign x and y to X and Y
    % no need to increment block, because you are at the end of the file

    if tline == -1
        X{block} = x;
        Y{block} = y;
    end

end

% close the file once all operations are done

fclose(file);

You now have a cell X and a cell Y which contain the data from your file. E.g.

X{1} = 

  Columns 1 through 2

                 54.51                    55.294

  Columns 3 through 4

                55.859                    55.999

I am aware that you won't get variables called x1, x2, ... xn with this approach. However, I believe that cells X{1}, X{2}, ... X{n} are both easier to generate, populate and handle than individual variables.

Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • Thank you very much, it worked. Now, I can apply `polyfit` using cell `X` and `Y` and get the intercept of the linear predictor. – dickyaz Jun 23 '14 at 20:35
  • @dickyaz I'm glad it works. Is that a follow-up question on how to use `polyfit` or do have that working? – Schorsch Jun 23 '14 at 20:41
  • I already figure it out how to calculate the intercept, I used this code: `for n=1:block` `p{1,n}=polyfit(X{n},Y{n},1);` `end` but there is notification 'Consider preallocating a variable or array before entering the loop by using zeros, ones, cell' do you have sugestion how to preallocating the cell `p` ? – dickyaz Jun 24 '14 at 07:20
  • @dickyaz - that's just a warning. typically you should be fine ignoring it in this case. however, you might find [this question/answer](http://stackoverflow.com/questions/2151636/matlab-consider-preallocating-for-speed) interesting. – Schorsch Jun 24 '14 at 11:29