How to extract rows which contains specific values in each row, then automatically repeat the process for a range.

i have a table of values imported from excel where one of the columns has a range of 0 to 8 . where there are several rows associated to each number through 0 and 8 . how can i extract the rows that have 0 in that column to do calulations , then repeat the proccess with the rows that have 1 in the same column and so on. would it require a loop ? how would the code be setup ?
as seen below all the results are te same because the calulations are being done to the whole column rather than the specified ones in red ( which should be equal to no_avg)
Capture.PNG

4 Comments

I would anticipate using a loop for this, but before we can talk about how we need to know how you have your data contained within matlab. You mentioned having an excel document, but you screen shot seems to indicate you are looking at iterating T as a single set of data multiple times.
this is my code so far , there was a mistake with the other results but the no_avg works fine
% the excel file is imported through the readtable below
t1 = readtable('test1.xlsx');
t1.Properties.VariableNames;
% here is the column of the excel sheet to be imported
X = t1{:,{'x0102ThrottleNotch'}};
n=0:8;
X(any(X == 0),:)
for notch = n % n number iterrations
fprintf(2,'No %i ',notch);
% Throttle Notch
throttle_notch = X;
no_avg = mean(throttle_notch);
throttle_notch_mode = mode(throttle_notch);
throttle_notch_min = min(throttle_notch);
throttle_notch_max = max(throttle_notch);
%fprintf(' Notch %i\n',throttle_notch_avg ,throttle_notch_mode,throttle_notch_min , throttle_notch_max );
T = table(no_avg)
end
Are you running into a specific error message? If so, please post the entire error message.
If not, please explain why you think the code is not doing what you want it to. What results are you seeing, and how do they differ from what you would expect?
there is no error message but the code is not doing what i want it to do, for example if a column has 100 total values , with the values ranging from 0-8 (13/100 read 0 , then 20 that read 1 , and so on) , the code is taking all 100 values and taking an average of all of them , where as i want the code to take only the values that read 0 and take that average. as well as record the rows where the values read 0 in order to do calulations in other columns. so as you see above for No 0 i want no_avg to read 0 where as it is taking all the values 0-8 and averaging them. below No 0 - 8 show the same values.Capture.PNG

Sign in to comment.

 Accepted Answer

The reason you're getting the same values is because "X" always equals the same thing:
X = t1{:,{'x0102ThrottleNotch'}};
n=0:8;
X(any(X == 0),:) % <-------------- this doesn't do anything. You're not assigning the value to a variable
for notch = n
throttle_notch = X; %< --------- this is always t1{:,{'x0102ThrottleNotch'}}
no_avg = mean(throttle_notch);
throttle_notch_mode = mode(throttle_notch);
throttle_notch_min = min(throttle_notch);
throttle_notch_max = max(throttle_notch);
T = table(no_avg)
end
Instead you want something like this
X = t1{:,{'x0102ThrottleNotch'}};
for notch = 0:8 %<----- loop through 0:8
throttle_notch = X(any(X==notch),:); %<----- change the row indices on each loop
no_avg = mean(throttle_notch);
throttle_notch_mode = mode(throttle_notch); % * See comment below
throttle_notch_min = min(throttle_notch); % * See comment below
throttle_notch_max = max(throttle_notch); % * See comment below
T = table(no_avg) % * See comment below
end
*These variables are being overwritten on each iteration of the loop. You should either use them within the loop or store them properly in a vector or matrix.

More Answers (1)

Without direct access to your data I cannot give you a perfect solution to your data, so what I put here will likely take some finessing to get it to work perfectly with your dataset. From what I understand you're mostly having trouble with identifing and selecting the data with specific values, as opposed to calculating the mean, or other calculations you want to make. Therefore, I am going to focus on that part.
As I mentioned, I think a loop will be simplest here.
range = unique(t1{:,{'x0102ThrottleNotch'}}); % Identify different values, incase they change
for i = 1:length(range)
r = find(t1{:,{'x0102ThrottleNotch'}}==range(i)); % Return rows for corresponding throttle notch value
... % Do you other calculations for each throttle notch here
end

Products

Release

R2014b

Tags

Asked:

on 10 Jun 2019

Edited:

on 11 Jun 2019

Community Treasure Hunt

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

Start Hunting!