2

I know that to use the load command I need to write:

blah = load('test.txt')

My problem is I need to skip the 1st few lines of my test file, i.e. the file is of the form

872
30
FR

(below here is data that needs to be put in matrix)

0000.0 0000.0 0000.0 etc...
0001.0 0000.0 0000.0 etc...
0002.0 0000.0 0000.0 etc...

So how do I do that?

I also have another file that looks like:

1 (abcdef)
2 (fedcba)
3 (edfbac)
on to 800 or so.

Is it possible load this kind of file in to a matrix? (N.b. I need to be able to look in the matrix to find the certain letter combinations, and then load a file the corresponds to the number associated with the letters).

user2587726
  • 169
  • 2
  • 11
  • Please, take a look at http://www.mathworks.com/matlabcentral/newsreader/view_thread/254504 – Papiro Jul 16 '13 at 14:24
  • possible duplicate of [Average of values from a column/ Skip lines (textscan)](http://stackoverflow.com/questions/8617405/average-of-values-from-a-column-skip-lines-textscan) – Eitan T Jul 16 '13 at 14:38

2 Answers2

3

Actually, load is for loading MATLAB data files (*.mat), see http://www.mathworks.co.uk/help/matlab/ref/load.html. For text files, best use textscan or dlmread.

am304
  • 13,758
  • 2
  • 22
  • 40
  • +1 textscan example: http://stackoverflow.com/questions/8414300/matlab-read-only-header-line-from-a-csv-file/8414375#8414375 – kol Jul 16 '13 at 14:34
  • Ok, so is there a way of reading the array textscan gives me so I can find the value that corresponds to a a certain string? I.e.say I've got a 1 b 2 c 3 etcetera, how do I get an output of 2 whenever there's a b in the 1st column? – user2587726 Jul 17 '13 at 22:32
1

You could also do this by reading the file in line by line, and deciding how many lines to skip with something simple like what we can see below.

fid = fopen('temp.txt','r');
count = 1;
lines2skip = 3;
mat = [];
while ~feof(fid)
    if count <= lines2skip
        count = count+1;
        [~] = fgets(fid); % throw away unwanted line
        continue;
    else
        line = strtrim(fgets(fid));
        mat = [mat;cell2mat(textscan(line,'%f')).'];
        count = count +1;
    end
end
fclose(fid);
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70
  • Thanks for that, you wouldn't happen to know how to write the lines I don't skip in to a matrix do you? – user2587726 Jul 18 '13 at 18:49
  • They're all the same (of the few I've seen, there's 2400 of them). 872 30 FR (<--Headers) 00000.0 0000.0 0000.0 (on to 872) 00001.0 0000.0 0000.0 etc 00002.0 0000.0 0000.0 etc Sorry I'm not sure how to enter a line break. – user2587726 Jul 18 '13 at 19:07
  • I can not really tell from here, please edit your question above to include an example of your data. – MZimmerman6 Jul 18 '13 at 19:22
  • yeah sorry it took me a while to respond, I see it. Give me a few minutes and I will update my answer – MZimmerman6 Jul 19 '13 at 10:59
  • Edited to make a matrix – MZimmerman6 Jul 19 '13 at 11:12
  • Cheers for that. Matlab gives me the error that mat is an undefined variable. I've tried to fix this by going p = 1:29, q = 1: 872, mat(p,q) = 0 (and thus create a matrix of zeros I assume), but it doesn't look right, got any more hints? Also, I posted another question, can you give me some more really good insight? http://stackoverflow.com/questions/17731851/generating-a-matrix-after-importing-data-from-csv-file-with-fgets-in-matlab Look at the comments under the question before making an answer. In them I explain the big picture a little better. – user2587726 Jul 19 '13 at 22:50
  • This seems to concatenate the matrix I made to get rid of the undefined variable error, rather than rewrite it, any clues? – user2587726 Jul 20 '13 at 13:15
  • to create a blank matrix simply use the `zeros` command, something like `zeros(29,872)` – MZimmerman6 Jul 22 '13 at 12:12
  • I fixed it ;) Can you lend a hand with this question? You've been so helpful. http://stackoverflow.com/questions/17787104/making-a-matrix-of-strings-read-in-from-file-using-feof-function-in-matlab – user2587726 Jul 22 '13 at 13:21