Info

This question is closed. Reopen it to edit or answer.

How do I store the list of 'Menergy' values that satisfy my while loop expression?Is my statement correct in taking each satisfied Menergy value and storing it in the array 'MeasuredEnergy? Menergy and Penergy are both 'read' from the same excel file

1 view (last 30 days)
Menergy =xlsread('Linear Regression Anaylsis.xlsx','First 6 Months','C252:C615');
Penergy =xlsread('Linear Regression Anaylsis.xlsx','First 6 Months','D252:D615');
MeasuredEnergy = [ ];
while (Menergy ~= Penergy) & (Menergy > Penergy)
MeasuredEnergy = [MeasuredEnergy;Menergy];
end
%Is it to do with my expression not calling the first variables in both vector, Menergy and Penergy? Sorry, novice matlab user %

Answers (3)

Geoff Hayes
Geoff Hayes on 1 Aug 2014
Cillian - no, your statement is not correct. You are using a logical vector as the condition for the while loop, which means that only if all elements in the logical vector are 1 (true) will you then enter the loop. Try putting the above code in a function or script, and add a breakpoint at the while line. When you run the code and step through it, you will notice that the MeasuredEnergy only gets set if all elements of the condition (Menergy ~= Penergy) & (Menergy > Penergy) are ones. Of course, if all elements are ones, then you will never exit the while loop either!
There are two ways to satisfy this problem. Either use a for loop and iterate over each element of Menergy and Penergy (doing a pair-wise comparison) to build the MeasuredEnergy, or do something similar to what you have but without any loop
% generate some random data (since I don't have your files)
Menergy = randi(100,615-252+1,1);
Penergy = randi(100,615-252+1,1);
% determine which indices in our two column vectors satisfy the condition
% (this will create the logical vector of zeros and ones like you have done
% already)
mGtPIdcs = (Menergy ~= Penergy) & (Menergy > Penergy);
% mGtPIdcs = Menergy Greater Than Penergy Indices
% now set MeasuredEnergy to those values of Menergy that satisfy the
% above
MeasuredEnergy = Menergy(mGtPIdcs);
MeasuredEnergy will be a column vector of all those Menergy values that are greater than their equivalent Penergy value. In fact, the following line
mGtPIdcs = (Menergy ~= Penergy) & (Menergy > Penergy);
could probably be rewritten as
mGtPIdcs = Menergy > Penergy;
Try the above and see what happens!

Cillian
Cillian on 5 Aug 2014
Thank you Geoff, I went for an If function and it worked perfectly. Thank you so much for establishing the right conditions for me.

Cillian
Cillian on 5 Aug 2014
Im trying to display my newly found vector 'MeasuredEnergy' with its corresponding date and a message. I have found how to identify the corresponding date but for some reason I cant display these dates with 'MeasuredEnergy'. Ideally I want an output display with the following:
Message : Date : MeasuredEnergy Value
Any help please. I tried fprintf and disp but none work.
Menergy = xlsread('Linear Regression Anaylsis.xlsx','First 6 Months','C252:C615'); Penergy = xlsread('Linear Regression Anaylsis.xlsx','First 6 Months','D252:D615'); [Num,Txt,Raw]= xlsread('Linear Regression Anaylsis.xlsx','First 6 Months','A252:A615');
meauredlesspredicted = (Penergy ~= Menergy) & (Penergy > Menergy);
MeasuredEnergy = Menergy(meauredlesspredicted);
if ((Penergy ~= Menergy) & (Penergy > Menergy))
MeasuredEnergy = Penergy(meauredlesspredicted);
end
c = []
for i = 1:length(MeasuredEnergy) index = find(Menergy == MeasuredEnergy(i)); c = [c, index(1)]; end
d = [];
dates =[];
for i = 1:length(c)
d = [d, Txt(c(i))];
dates = d.';
end
  5 Comments
Geoff Hayes
Geoff Hayes on 6 Aug 2014
Cillian's comment
Thank you Geoff, but that still doesnt display dates, MeasuredEnergy together. For example when I
Message = sprintf('Date is %s, Measured value is %6.2f. \n',dates, MeasuredEnergy(i));
I get an error saying that:
Error using sprintf Function is not defined for 'cell' inputs.
Any ideas?
Geoff Hayes
Geoff Hayes on 6 Aug 2014
Cillian - in the above code, you are iterating over each element in the MeasuredEnergy array as
for k = 1:length(MeasuredEnergy)
Message = sprintf('Date is %s, Measured value is %6.2f. \n',dates, ...
MeasuredEnergy(k));
end
Note that I changed the i to k as MATLAB uses i and j to represent the imaginary number.
Now since the code is grabbing the kth measured energy value, it must grab the kth date value too
Message = sprintf('Date is %s, Measured value is %6.2f. \n',dates{k}, ...
MeasuredEnergy(k));
Given the error message, I am guessing that the dates variable is a cell array, and that each element is a string.
Try the above and see if it helps, else you will have to post your data.

Community Treasure Hunt

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

Start Hunting!