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.
for i=0:3
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!

Answers (1)

Mathieu NOE
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
% 0.1854
% 0.1861
% 0.1846
% 0.1913
% and you're good to continue (values, nb of rows and columns, ...)
Mathieu NOE
Mathieu NOE on 26 Nov 2020
I had to read it quite numerous time , and it's still difficult for me to really understand what you want to do
but since there is an easy and smple way to have access to all the data , I thought that it would then be quite simple to do whatever you want on the data
good luck

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!