0

I am a new matlab user trying to load my 145 .csv-files into matlab. The files have similar names and all contain two columns and 3000 rows. I need to be able to work with column 1 and 2 separately for each file and to plot them (column 2 over 1). So far, I tried the following (for the folder containing all files):

clear ;
direc = dir('*.csv');               
for i=1:length(direc)               
  x = csvread(direc(i).name,1);     
end

My x is only of 3000x2 double, but I need the third dimension...and I do not know how to access the 'direc' folder. I also tried to define column 1 and 2 of each file extra by:

time(i,:,:)=x(:,:,1) and 
signal(i,:,:)=x(:,:,2)

and to plot it; but it returns a plot only for 1 dataset (1file).

Can someone help me with that? I hope I gave all necessary information.

Thanks!

Shai
  • 111,146
  • 38
  • 238
  • 371
Susu
  • 1
  • 1
  • 1

1 Answers1

2

I am not sure what you mean by "how to access the 'direc' folder" (direc is a struct array, and you are already accessing it by direc(index).fieldname as intended), but I think the main problem is that in the for loop, every iteration overwrites the previous content of x, so what you get in the end is the content of the last read file.

If all your csv files have exactly the same dimensions, what you need seems to be

x(:,:,i) = csvread(direc(i).name,1);

You can then access all first columns by squeeze(x(:,1,:)) - only x(:,1,:) will return a three-dimensional array of size 3000x1x145 (using the numbers you give above), squeeze will produce a more convenient 3000x145 matrix.

If not all of your csv files have the same number of rows and columns, you need to use a cell array and the convenient indexing can not be used (btw: did signal(i,:,:)=x(:,:,2) really work on a two-dimensional x?), or fill with zeros, which may require an intermediate step to find how large the 3D-array should be in the end.

arne.b
  • 4,212
  • 2
  • 25
  • 44
  • Hi,and thanks a lot for your comment! Seems like the loop was really the problem. But now, if I try to do it without the loop, it gives me the error message "??? Error using ==> csvread Too many input arguments." But since I haven't really defined anything, I can not think of a wrong code...and there are hardly too many data. Do you have an idea again? – Susu May 27 '11 at 06:25
  • Are you still using `csvread(direc(i).name,1);` with scalar index `i`? If you enter only the argument of the function (e.g. `direc(i).name,1` in the above case) without `csvread`, parentheses and semicolon, what is the output? – arne.b May 27 '11 at 07:12
  • then I get all the names of the files.csv so a 145x1 structure – Susu May 27 '11 at 07:32
  • Then `i` is not scalar. The line I gave in the answer was meant to replace the line inside the `for` loop of your code segment, not the entire loop. Inside the loop, at every iteration `i` is exactly one value from 1 to 145 and `direc(i).name` should only be one csv file name. Sorry if that was confusing. – arne.b May 27 '11 at 07:50
  • ok, that is clear. I had actually tried this before. It always gives me the error: "??? Subscripted assignment dimension mismatch." I probably did something really basic wrong, just can not find out what it is...And it again gives me only 2dimensional results. – Susu May 27 '11 at 08:23
  • Yes! I think I found my problem, it was just the most typical one: wrong naming in some of the files! And then, your suggestions help me to produce the 3dim. structures! Thanks so much! – Susu May 27 '11 at 09:33