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

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)

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!
Thank you Geoff, I went for an If function and it worked perfectly. Thank you so much for establishing the right conditions for me.
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

Cillian - your above statement is not an answer to your original question. So please use the Comment section to further the dialog, or create a new question for this new problem.
Looking at your above code, it isn't clear what you have tried to do to print out the date. What have you tried that is not working? Where is the date information stored?
*Apologies Geoff for my lack of clarity. Here is my revised code. My problem is this. I need to compare two excel columns that meet my condition below. Thanks to your help, that done using the if loop.
Now for each comparison there is a associated date located in a separate date column. My aim is to fprintf or disp a message for every comparison; the value of the comparison (done already), an error message (done already), and the date. Im having problems displaying the corresponding date.*
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) & (0.8*Penergy > Menergy);
MeasuredEnergy = Menergy(meauredlesspredicted);
if ((Penergy ~= Menergy) & (0.8*Penergy > Menergy))
MeasuredEnergy = Menergy(meauredlesspredicted);
end
for i = 1:length(MeasuredEnergy)
%index = find(Menergy == MeasuredEnergy(i));
% c = [c, index(1)];
Message = sprintf('PV Cell under performed, Measured value was %6.4f kWh for today. \n',MeasuredEnergy);
end
d = [];
for i = 1:length(c)
d = [d, Txt(c(i))];
dates = d.';
end
Note that my second for loop will find each of the dates for the comparison. And if I wanted I could print an error message with that too. But I want all three displayed together. Right now my output is;
_Message =
PV Cell under performed, Measured value was 0.0000 kWh for today.
PV Cell under performed, Measured value was 0.0680 kWh for today.
PV Cell under performed, Measured value was 0.0000 kWh for today.
.........etc etc_
Why do you still have the if statement
if ((Penergy ~= Menergy) & (0.8*Penergy > Menergy))
MeasuredEnergy = Menergy(meauredlesspredicted);
end
as the two previous lines should be sufficient? If you want the dates associated with the MeasuredEnergy values, then wouldn't it simply be
dates = Txt(meauredlesspredicted);
which could be put with
meauredlesspredicted = (Penergy ~= Menergy) & (0.8*Penergy > Menergy);
MeasuredEnergy = Menergy(meauredlesspredicted);
dates = Txt(meauredlesspredicted);
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?
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.

This question is closed.

Asked:

on 1 Aug 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!