MATLAB Answers

0

How to select specific cells rows in a column

Asked by Louis Cook on 4 Jan 2019
Latest activity Commented on by Cris LaPierre on 7 Jan 2019
So i currently have an array that is similar to this on a much larger scale:
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
I need to write a code that picks out all of the zeros in colum 5 ( the ones that are bolded) but I need it to ignore any subseuent zeros after that (the two rows after)
any help would be great

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by per isakson
on 5 Jan 2019
Edited by per isakson
on 6 Jan 2019

%%
M = [
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
];
%%
isZero = M(:,5)==0;
% position of first zero
ixb = find( isZero, 1, 'first' );
% position of last zero in first sequence of zeros
ixe = find( not(isZero(ixb:end)), 1, 'first' ) + ixb - 2;
%%
z5 = reshape( M(ixb:ixe,5), 1,[] )
returns
z5 =
0 0 0

  1 Comment

Sorry didnt know to re ask in here :)
So i tried the above option into my code but it wasnt really working so I guess its easier to put my entire code into this and hopefully there is an easier solution :)
1.csv is an example of a file in which there is only one group of 0's so there is no problem
10.csv is an example of where this is two groups of 0's which is causing a problem
thanks for the help
g=9.81; %acceleration due to gravity
num = 1:78;
ext = ".csv";
JumpData = num + ext;
for f=1:length(JumpData );
inputdata=xlsread(JumpData{f});%extract all data from first jump%
idx = find(inputdata(6:3000,5)==0);%finds all the zeros in the z collumn%
zeroMatrix = inputdata(idx,5);%Turns it into a matrix%
oneMatrix=zeroMatrix+1;%adds to to all the zeros%
t=sum(oneMatrix)/100;%sum all of the zeros and turn into miliseconds%
MH(f)=0.5*g*(0.5*(t^2));
MHFinal=MH';
end

Sign in to comment.


Answer by Cris LaPierre on 6 Jan 2019
Edited by Cris LaPierre on 6 Jan 2019

I'm a fan of readtable, so although it takes more setup, I think overall it handles tables better.
Here is code that just finds the first set of zeros. I'll let you add the rest of your code to it.
for f=1:length(JumpData)
% Set import options to capture information in headerlines
opts = detectImportOptions(JumpData{f});
opts.VariableDescriptionsLine = 3;
opts.VariableNamesLine = 4;
opts.VariableUnitsLine = 5;
opts.DataLines = 6;
% load data into a table
data = readtable(JumpData{f},opts,'ReadVariableNames',true);
% find first grouping of zeros in Fz
idx = find(data.Fz == 0);
step = find(diff(idx)>1,1)+1;
idx(step:end)=[];
% convert length of zeros to time aloft (each 0 corresponds to 0.01 seconds?)
tm(f) = length(idx)*0.01;
end

  2 Comments

EDIT: Changing your answer to a comment
Answer by Louis Cook on 6 Jan 2019 at 11:55
Hello thankyou for your response,
the code works near enough perfect :) the only problem is that the output (tm(f)) is coming out a 1x2 matrix and i dont see why as it is only asking for the length of idx.
How many files were in JumpData? I'm guessing 2? The code I wrote will make tm a 1xf vector, where f is the number of files in JumpData.
I naively assumed you would want to capture the time for each file. The variable f is your loop counter, so tm(1) is the time in JumpData{1}, tm(2) is the file in JumpData{2}, etc.
If that is not the case, you will need to modify the code I shared to fit your use case.
You can learn more about indexing here.

Sign in to comment.