Prev: How to create an image from a binary mask file using RESHAPE?
Next: Adjust colorbar scale on quiver plots
From: Gabriele on 18 Apr 2010 15:13 Hi everyone, I am trying to import a txt file which has a variable number of rows separated by a blank line and a line with the variable names x and y. The first lines look like this: x y -0.5 0.00001778 -0.48 0.00001865 -0.46 0.00003412 ......more rows... 1.5 0.00454982 x y -0.5 0.00001549 ......more rows... 1.5 0.00754182 x y .... .... I would like to import this file in such a way that I have all column arrays made of all the x and y vectors above or a cell array with each cell containing the corresponding x and y vector. Here there is my first try to import the mentioned txt file: fid = fopen('curves.txt'); %Total number of headers that I would like to know tot_header = ??; for k=1 : tot_header N_header= (vector specyfing the line after which the new set of variables start) ; C{k} = textscan(fid, '%f%f', 'headerlines', N_header); end fclose(fid); The only problem lies in the headerlines as it is a variable number as well as the number of rows separated by each x and y is variable. Is there a way to tell matlab to import a new cell after the it encounters the 'x' and 'y' in the txt file? Of course, there might be easier ways to solve this problem, this is the only one I could think of so far. Thanks.
From: Rune Allnor on 18 Apr 2010 15:58
On 18 apr, 21:13, "Gabriele " <gabriele...(a)gmail.com> wrote: > Hi everyone, > I am trying to import a txt file which has a variable number of rows separated by > a blank line and a line with the variable names x and y. The first lines look like this: > > x y > -0.5 0.00001778 > -0.48 0.00001865 > -0.46 0.00003412 > .....more rows... > 1.5 0.00454982 > > x y > -0.5 0.00001549 > .....more rows... > 1.5 0.00754182 > > x y > ... .... > > I would like to import this file in such a way that I have all column arrays made of all the x and y vectors above or a cell array with each cell containing the corresponding x and y vector. > > Here there is my first try to import the mentioned txt file: > fid = fopen('curves.txt'); > %Total number of headers that I would like to know > tot_header = ??; > for k=1 : tot_header You can not do things like that. You will have to use a WHILE loop, not a FOR loop. More or less something like: 1) Open file 2) Read a line from the file 3) Check if the line is empty (or contains only blank spaces) 4) Read the next line 5) Check that the line contains only the letters 'x' and 'y' (in addition to blank spaces or tabs) 6) Read the next line 7) Check if this line contains two numbers If 'no' repeat from 3) 8) Store the numbers into a cell 9) Repeat from 6) Rune |