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
How do I return the first instance the value goes below 0 from a specific cell start point?
12 ビュー (過去 30 日間)
古いコメントを表示
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??
回答 (2 件)
dpb
2014 年 5 月 14 日
[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...
dpb
2014 年 5 月 15 日
編集済み: dpb
2014 年 5 月 15 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!