How do I return the first instance the value goes below 0 from a specific cell start point?

6 views (last 30 days)
This is my code:
clc clear all close all
%% Import data
numfiles = 54; % number of excel files
mydata=cell(numfiles,1); % defining size of mydata
fileinfo = xlsread('Group B File Info');
d=dir('Trial*.csv');
for i=7:length(mydata) % loop to import mutliple excel files
try
if d(i).bytes == 0
continue; % Skip this one file because it is empty
end
mydata{i} = xlsread(d(i).name); % import files into mydata
catch
disp([d(i).name ' read failed'])
end
myfilename = sprintf('Trial%02d.csv', i); % define file name
mydata{i} = xlsread(myfilename); % import files into mydata
%% Perfrom Calculations
%%Define variables
a= 9.81; % acceleration
fps = 250; % frames per second
[y,z] = size(mydata{i,1});
%%Calculate Jump Height
firstz = find(mydata{i,1}(:,5)== 0,1,'first');% to find the first zero in the column
fz = firstz+1;
lastz{i,1} = find(mydata{i,1}(fz:y,5)<0,1,'first');% to find the last zero in the column
no_of_zeros{i,1} = lastz{i,1} - fz;
no_of_frames = (no_of_zeros{i,1}/4); % number of frames
tof = ((no_of_frames/fps)/2); % time up
jumph(i,1)=((a*(tof*tof))/8); % jump height
I am trying to define the first zero in the data which has worked then the first appearance of when the value goes below 0 starting from the firstzero point. However this line in my code doesn't work.
When I run the define varaibles section y comes out at 6004 which is correct, when I run the whole script y is returned at -0.6004 which I think is why the lastz value is incorrect.
How would I fix this??
  1 Comment
Franchesca
Franchesca on 15 May 2014
The data variable my data is a string so I open that then across column 5 in each cell. The way the data works is it starts with numbers then there is a block of zeros (which I have found the starting one) then it will return to numbers all below zero, there may also be extra zeros at the end of the column. Henc

Sign in to comment.

Answers (2)

dpb
dpb on 14 May 2014
[y,z] = size(mydata{i,1});
firstz = find(mydata{i,1}(:,5)== 0,1,'first'); % first zero in the column
fz = firstz+1;
lastz{i,1} = find(mydata{i,1}(fz:end,5)<0,1,'first'); % 1st <0 after zero
Not sure after here but lastz isn't quite what your comment says unless your data has the characteristic of there being no more zeros by definition.
It's the location of the first value <0 from the reference point fz (emphasis added)
Hence, anything in the overall series from there on must have the offset added to get to it or to have 1-based addressing for the remainder of the series after finding this location, save this portion to the end in a new variable or remove from base location 1:lastz-1.
I don't understand how y can change--you defined it as the size() value and it's never redefined in the code you show.
Need some more clarification on what went wrong and how...
  1 Comment
Franchesca
Franchesca on 15 May 2014
I am trying to find the last zero in the block of zeros for which I have foun the first zero (if that makes sesne). As there may be zeros at the end of the column I cannot just find first and las zero.

Sign in to comment.


dpb
dpb on 15 May 2014
Edited: dpb on 15 May 2014
Ah, that's more easily understood what looking for--
find(x(fz:end),1,'first')
will return the first nonzero location from that location in a vector x. The last zero would be the location prior to that but I presume it's really eliminating the zero block that's your objective so the first nonzero value is likely the one you actually want.
Remember to add the offsets of these locations to the index to get absolute position in the full vector as find counts from 1 inside the vector it's working on, not caring about those positions you've already scanned over previously (or again as noted above, for simplicity in later code you could simply save this point on to the end and have a one-based vector to work with going forward).
Another hint: Think about what you get in the series if you were to apply
z=[0 diff(x~=0)];
and how that might help isolate a block of zeros. Try some experiments on a short sample vector in your workspace that you can watch the results.
Also, if sign is important, turn the vector into
z=x;z(z<0)=-1;z(z>0)=1;
and diff z. Again, try it on a sample small enough to be able to observe. It's beginning to think of these kinds of "tricks" to manipulate data that are the thing to take away; they're useful everywhwere.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!