Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>FID = fopen('datafile.dat'); data = textscan(FID,'%f %f %f %f', 'headerLines', 6); fclose(FID); </code></pre> <p><code>data</code> will be a 1x4 cell array. Each cell will contain a 3x1 array of double values, which are the values in each column of your data.</p> <p>You can access the 2nd and 4th columns of your data by executing <code>data{2}</code> and <code>data{4}</code>.</p> <hr> <p>With your original code, the main issue is that the data file has <em>6</em> header lines but you've specified that there are only <em>4</em>.</p> <p>Additionally, though, you'll run into problems with the specification of the number of times to match the formatSpec. Take for instance the following code</p> <p><code>data = textscan(FID,'%f',4);</code></p> <p>which specifies that you will attempt to match a floating-point value 4 times. Keep in mind that after matching 4 values, <code>textscan</code> will stop. So for the sake of simplicity, let's imagine that your data file only contained the data (i.e. no header lines), then you would get the following results when executing that code, multiple times:</p> <pre><code>&gt;&gt; FID = fopen('datafile_noheaders.dat'); &gt;&gt; data_line1 = textscan(FID,'%f', 4) data_line1 = [4x1 double] &gt;&gt; data_line1{1}' ans = 1 2 3 4 &gt;&gt; data_line2 = textscan(FID,'%f', 4) data_line2 = [4x1 double] &gt;&gt; data_line2{1}' ans = 5 6 7 8 &gt;&gt; data_line3 = textscan(FID,'%f', 4) data_line3 = [4x1 double] &gt;&gt; data_line3{1}' ans = 9 10 11 12 &gt;&gt; data_line4 = textscan(FID,'%f', 4) data_line4 = [0x1 double] &gt;&gt; fclose(FID); </code></pre> <p>Notice that textscan picks up where it "left off" each time it is called. In this case, the first three times that textscan is called it returns one row from your data file (in the form of a cell containing a 4x1 <em>column</em> of data). The fourth call returns an empty cell. For the usecase you described, this format is not particularly helpful.</p> <p>The example given at the top should return data in a format that is much easier to work with for what you are trying to accomplish. In this case it will match four floating point values in each of your rows of data, and will continue with each line of text until it can no longer match this pattern.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload