Error in locating a reference cell for calculations

So far I have got this code:
%%=================================================
%%to find average power first use "find" function to find the first zero in
%%Fz, have the cell referenced
%%then use nanmean for average power(av_pwr)
%%use nanmin for peak power (peak_pwr)
%%=================================================
ref= find(data{i,1}(:,5)==0);
ref=ref(1);%returns all times zero occurs then extracts first time data is 0
peak_pwr(i,1) = nanmin (data {i,1}(1:ref,5));
%preak power in coloumn E1 in all data with reference to cell found
av_pwr(i,1)=nanmean(data{i,1}(1:ref,5));
%average power in coloumn E1 in all data with reference to cell found
I have tried to locate the first zero in column E1 and then use this as a reference cell to get the peak and average power. However it is coming up with this error:
Attempted to access ref(1); index out of bounds because numel(ref)=0.
Error in code (line 53) ref=ref(1);%returns all times zero occurs then extracts first time data is 0
anyone know how to get around this?

Answers (1)

If you read the line:
Attempted to access ref(1); index out of bounds because numel(ref)=0.
you can see that
data{i,1}(:,5)==0
is never fulfilled and therefore, ref remains an empty array with no values in it.
When you tried to access its first value:
ref=ref(1)
the system complains (yields an error) because there is nor such an element. Since I do not have data, it is nor easy to tell whether any of its elements equals 0 in any moment.

5 Comments

Sorry, I still don't quite understand what is it that I am doing wrong? I've referenced my data set to find all the zeros in the cells, so i want it to reference the cell that has the first data starting at zero. so then i can use the data before that first zero to calc. peak and average power?
I now understand that it is just an array of zeros its collecting and so it says its empty but I want find the first cell where it is zero as a reference cell so that I can use all cells up to this to find the peak power and average power from the Fz column. I've attached an example data so it might make it clearer.
the problem is in your data cell array.
In your line
ref= find(data{i,1}(:,5)==0);
who is i ? What's the type of data within data? If they are strings, ref will always be an empty array.
how are you reading the CSV file?
(i) is for the loop so that it stores the information rather than write over it each time as I have to know all the data for each 81 jumps. i had trouble reading them in so I had to do it like this:
i=1:njump;
x=sprintf('Trial%02d.csv',i),sprintf('Trial%02d',i),'A1:E7000';;% jump data
data{i}=xlsread(x,'A1:E7000');
% code
end

This question is closed.

Asked:

on 14 May 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!