Move to the next row when reading the .csv file using csvread()
18 views (last 30 days)
I have a .csv file and I want to read data row by row from it. The file is attached so that you could see how it looks like. Below is also a screen shot:
I want to obtain the values of each cell of each row and when the program reaches the end of each row (where the cells are empty or basically zero), I want the program to move to the next row and starts reading the next row till it reaches the end of next row and moves to the next row and this process repeats until all the rows are read and collected. I have several .csv fiels and and want to operate this process on them; since the data in each .csv file is different, the end cell of each row is different from a .csv file to another .csv file (i.e. the number of end cell in all the .csv files is not the same); therefore, I need to ask the program to read the value in each cell and compares if it is ==0 (which means we are at the end of the row and now we should go to the next row) or not. Do you have another idea of how to move to the next row (like I said, we don't know where the end of current row is and we should look for the empty cell to understand where the end of row is)? The other solution came to my mind was to use "end", but it does not work in csvread() command.
My current program is as below; it works now, because I know where the end of each row is (column 6) but without knowing where the end is, I need to open each .csv file manually and find the number of last cell in each row.
m(j,:)= csvread(data, i, 1, [i,1,i,6])
% I tried m(j,:)= csvread(data, i, 1, [i,1,i,end]), but it doesn't work and gives the error:
% "The end operator must be used within an array index expression". ANy idea how to find the end of each row
% or how to incorporate the end operator to csvread() ?
Any help is appreciated!
Mathieu NOE on 25 Nov 2020
why making this so overly complicated ?
m = csvread('data_points.csv')
% gives :
% m =
% Columns 1 through 6
% 1.0000 0.2074 0.2130 0.2046 0.1899 0.1844
% 2.0000 0.2078 0.2112 0.2016 0.1901 0.1852
% 3.0000 0.2059 0.2092 0.1950 0.1907 0.1874
% 4.0000 0.2043 0.2057 0.1981 0.1924 0.1930
% Column 7
% and you're good to continue (values, nb of rows and columns, ...)