Variable error on second file read when going through a sequence of files

1 view (last 30 days)
Hi.
I have a working code for a single file which extracts data from a csv file, performs some calculations and then exports as a .txt file. I have another code where it runs through all the files in a directory so I have amended that bit of code to this new code. However, it performs fine for the first file in the directory and then gives me an error of:
Index exceeds matrix dimensions.
at one of my variables. There must be something wrong on the next loaded file or it's being messed up by the variables from the first file. If I comment out the variable it progresses until another line of code, one of the calculations and gives a different error of:
Non-finite endpoints or increment for colon operator in index
My code is something like this
clear;
clc;
Files = transpose( dir( '*.csv' ) );
for file = Files
disp( file.name )
FileName1 = (file.name)
[num text raw] = xlsread(FileName1);
% Variables are along these lines etc
detection = num(5,2);
Voltage = num(12,5);
date = text(14,3);
myCode;
output to .txt file;
end
The error first appears at the variable:
date = text(14,3);
on the second file in the sequence. If I comment out that variable it runs until one of the equations later in the code. I really don't know what's going on, any ideas?

Accepted Answer

Star Strider
Star Strider on 15 Sep 2014
I’m surprised any of your index references work at all. MATLAB does not automatically decrement indices unless you tell it to. If you don’t tell it specifically, it returns an empty matrix:
x = 10:20;
yi = x(5:2) % Implied Increment
yd = x(5:-1:2) % Declared Decrement
Also, I’m curious as to your use of transpose here:
Files = transpose( dir( '*.csv' ) );
The reason for that eludes me.
  7 Comments
Ross
Ross on 16 Sep 2014
UPDATE: Oh dear, removing that troubled file makes my code work as intended. Haha! Every day is a school day. Thanks for taking the time to help! That was such a rookie mistake haha.
Thanks it's working fine now.
Star Strider
Star Strider on 16 Sep 2014
My pleasure!
You probably need to import the errant file manually, then save it and its appropriate variable fields as a .mat file.
I would do that with all of them. Then you simply load them as necessary, and not worry about reading the spreadsheet files each time.

Sign in to comment.

More Answers (1)

Titus Edelhofer
Titus Edelhofer on 15 Sep 2014
Hi,
some comments: first of all your indexing is wrong: 5:2 is the empty set. Try 2:5 instead (same of course for Voltage, date):
Second: do all files have the same input? If you get an index error, typically this is due to files having different sizes (and therefore num, text, raw having different sizes).
Titus
  1 Comment
Ross
Ross on 16 Sep 2014
Upon stepping through my code, I can see that after the first file is processed, on the second file, read using:
[num text raw] = xlsread(FileName1);
The size of all the num, raw and text elements is wrong which is giving me the error. I don't understand why though, this code works fine on other files. I'm wondering if it is some csv issue with multiple files or something.

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!