MATLAB: For loop that extracts non constant values from a column in an Excel table and outputs the desried value of the selcted row

for loopnon constant values

I am writing a code, and I need to add a for loop for one of my parameters (DNI) as it is non-constant. I have a table of values in Excel that displays each hour of the day (1-24) and a corresponding value of DNI for that hour. So for example: Hour 1 (which represents 12am-1am) has a DNI value of 0 W/m^2. I need to make a loop that allows me to assign a specific value of h, where h goes from 1 to 24, so that I can output the specific value of DNI corresponding to that hour (h). This is a small part of my code and I've been trying different things, but I can't seem to implement a loop for my case:
% For q
num = xlsread('Testing.xlsx');
tbl = num ;
col_2 = tbl(:,2); % Column of values
DNI = col_2(14) % At the moment I just popped in the number of 14 to give the value of the 14th row from my DNI column
D = 7; % Dish diameter - you can ignore this
A = (3.14*D^2)/4; % Area of dish - % you can ignore this
q = DNI*A*nopt % qin equation -% you can ignore this
I'm just doing 1-24 hrs for now. The actual table will have 8760 rows (for each hour in the year, rather than in the day). Which is why I'm using Excel to store my values.
Does anyone know how I can do a loop for these non-constant values? So if I ask MATLAB for hour 5 or 14 or 23 for example – it can give me DNI at hour 5 or 14 or 24? (Which in turn will give me q for that hour, etc).
I have attatched an excel file with the table for refrence.
Many thanks in advance.

Best Answer

Use logical operators for this purpose
num = xlsread('Testing.xlsx');
tbl = num ;
col_2 = tbl(:,2); % Column of values
val = 5; % find value at 'Hour=5'
ix = abs(val-col_2) < 0.1; % find hours close to 'Hour=5'
DNI = col_2(ix); % find appropriate DNI