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

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

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

Whoops! Sorry guys, massive typo's, my variables should be:
detection = num(5,2);
Voltage = num(12,5);
date = text(14,3);
as they are referencing information in cells.
As for the transpose code, it eluded me actually, someone suggested I try it for my last code and it worked well. I was just trying to adapt it to my new code.
If you’re getting the error in the line:
date = text(14,3);
you need to test the size of the text variable. You may need to search for specific characteristics of the date to locate it in the variable, since it doesn’t appear always to be where you want it to be.
Ok, I have stepped through the code and for some reason, after the first file is done, the num, text and raw matrices all change size, and text has a size of 1 cell. I don't understand why this is happening as I use this with other files and it works fine?
It should just do a new [num text raw] for the new file loaded. I'm obviously missing something here.
It’s difficult to be specific without knowing the details. A (1x1) cell tells you the size of the cell, not the contents:
x = {datestr(now, 'yyyy-mm-dd')}
sz = size(x)
produces:
x =
'2014-09-16'
sz =
1 1
You likely have to look at the file that is causing the problem to see how to work with it. The text obviously changes between the files, and the files don’t all have the same format.
I'll try and elaborate. On your example there, I type the two lines and all is well.
On matlab's workspace if I double click the variable 'x' it opens a window on matlab showing me the date in cell (1,1). This is fine and if I run my code on one file, I get a similar output. Also, if I double click the 'text' variable in the workspace view, I get a big table showing all the csv data. However, when I loop round to process the second file and it carries out the [num text raw] line, if I double click the text variable now, the csv data for the second file isn't there and in fact, the num and raw variables show major differences as well.
There must be something going wrong with my for loop or something that occurs after the first file is processed.
EDIT: Oh my! I just re-read what you wrote and checked the file it's tripping up with and there is a problem with THAT file, the layout is messed up. I'll remove it and try again and update.
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.
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)

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

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

Asked:

on 15 Sep 2014

Commented:

on 16 Sep 2014

Community Treasure Hunt

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

Start Hunting!