'Array indices must be positive integers or logical values' error for directory use

Hello Everyone,
I am currently undergoing a project in which I must create a code that analyses a potentially infinite amount of data. As such, I am using a directory and the csvread function in conjunction. I have been told that the continued use of csvread in my code slows down the code and makes it less efficient so I am therefore trying to load the csvread function into the RAM and create a shortcut that links it to my directory. I, however, keep getting an error saying 'Array indices must be positive integers or logical values.' Please see the relevant section of my code attached. Thanks.
%% Import and Sort Data
files = dir('\\nask.man.ac.uk\home$\Vortex Shedding\Used Data\*csv');
[r inx] = sort({files.name});
files = files(inx);
(csvread(files(i).name)) == Signal
%% Calculate Turbulence Intensity
for i=1:length(files)
ave = mean(Signal);
Average(i,:) = ave(:,3);
RMS = sqrt((sum((((Signal)-mean(Signal)).^2)))/(length(Signal)));
RootMeanSquare(i,:) = RMS(:,3);
TurbulenceIntensity(i,:) = (RootMeanSquare(i)/Average(i))*100;
end

2 Comments

What is this supposed to be doing?:
(csvread(files(i).name)) == Signal
Given that i is not defined previously in your code, its value is going to be sqrt(-1), which is unlikely to be very useful as an index. Even if you use a valid index, this line seems to import some data and then compare it against Signal... and then discard those results (because you did not assign the output to any variable). Why do you want to throw away the result of that comparison?
"I am therefore trying to load the csvread function into the RAM and create a shortcut that links it to my directory."
Data can be imported (or loaded) from a file into MATLAB's memory. A function is called or executed. What does it mean to "load" a function?
What exactly is "a shortcut that links it to my directory" ?
I have probably misworded my question as I am unfamiliar with the jargon used in programming.
This is my original, working code, that loads and reads csv files from my directory and calculates the turbulence intensity.
%% Import and Sort Data
files = dir('\\nask.man.ac.uk\home$\Vortex Shedding\Used Data\*csv');
[r inx] = sort({files.name});
files = files(inx);
%% Calculate Turbulence Intensity
for i=1:length(files)
ave = mean(csvread(files(i).name));
Average(i,:) = ave(:,3);
RMS = sqrt((sum((((csvread(files(i).name))-mean(csvread(files(i).name))).^2)))/(length(csvread(files(i).name))));
RootMeanSquare(i,:) = RMS(:,3);
TurbulenceIntensity(i,:) = (RootMeanSquare(i)/Average(i))*100;
end
I have been told by my lecturer that continuously using the csvread(files(i).name) function will slow down my code as it has to repeatedly read data from the computers harddrive. He also told me that its possible to create a function that recalls the csvread function, so that the code does not repeatedly have to read from the computer each time it is used.

Sign in to comment.

 Accepted Answer

"I have been told by my lecturer that continuously using the csvread(files(i).name) function will slow down my code as it has to repeatedly read data from the computers harddrive."
True. You call csvread four times inside the loop, and because reading/writing from any drive is slow, this will considerably slow down your code. You can avoid this very simply by loading the file data once at the start of the loop:
for i=1:length(files)
mat = csvread(files(i).name); % load once!
ave = mean(mat);
Average(i,:) = ave(:,3);
RMS = sqrt(sum((mat-ave).^2)/length(mat));
RootMeanSquare(i,:) = RMS(:,3);
TurbulenceIntensity(i,:) = RootMeanSquare(i)/Average(i)*100;
end
I also removed the superfluous parentheses which were cluttering everything up.
" He also told me that its possible to create a function that recalls the csvread function, so that the code does not repeatedly have to read from the computer each time it is used."
I guess he is referrring to memoize, but if you only call csvread once at the start of the loop for each file (as my code shows) then there is no real point to using this.
If you are interested in writing robust, efficient code then you should preallocate Average, RootMeanSquare, and TurbulenceIntensity:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!