How to get a csv file excluding first row and last column to run through if statement?

I'm new to matlab so I hope this makes sense!
The first row is headings and the last column is not needed for the if statement. I need the file to run through the ifelse statement without defining the number of rows so that numerous different files can run through it at different times with me just changing the file name.
I have been using the following code:
fid = fopen('test.csv');
data = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %*s','Delimiter',',','Headerlines',1);
fclose(fid);
numrows = size(fid,1);
for rownum = 1:numrows;
thisrow = fid(rownum,:);
val1 = thisrow(1);
val2 = thisrow(2);
val3 = thisrow(3);
val4 = thisrow(4);
With val1 etc being the column data for the ifelse statement. There are 13 columns (val1...val13).
However the error below keeps appearing:
Index exceeds the number of array elements (1).
Error in test2 (line 8)
val2 = thisrow(2);
Does anyone have advice that can help me?
Thanks

 Risposta accettata

  • fid is your file identifier
  • data holds the data of the file in a cell array
Thus replace the lines
numrows = size(fid,1);
thisrow = fid(rownum,:);
by
numrows = size(data,1);
thisrow = data{rownum,:};

Più risposte (2)

Let's fix the biggest issue first. Should be
numrows = size(data,1);
thisrow = data(rownum,:);
Thank you both for your help, it is now coming up with an error in my if else statement but thank you for helping me get one step closer

1 Commento

My standard answer
* See <https://se.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html Debug a MATLAB Program>
* See <https://se.mathworks.com/help/matlab/matlab_prog/examine-values.html Examine Values While Debugging>
Too bad copy&paste of hyperlinks doesn't work with the new Answer Editor

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by