# Matlab bins with two features: calculation and plotting

I have the following code in Matlab, which is supposed to do what I asked in this SO question. The idea is to:

1. Divide the temperature deadband of each agent (they are different between agents) into N_bin/2 bins.
2. Then have N_bin total bins to represent both normalized temperature from deadband and the ON/OFF switch state.

Both these points are done in the function Offline_Markov, which I am not sure if does the separations correctly.

Note: it is important that the bins handle the cases where the temperature is outside the deadband correctly (this is, temperature smaller than lower end of smallest bin/ temperature bigger than upper end of biggest bin). In this case they shouldn't be discarded, but rather stored in two "artificial" bins.

1. Plot the number of agents in each bin and ON/OFF status over one day measured at X minute intervals.

Point number 3. is done in the function Plotting_Markov (scroll down), and a typical function call looks like: Plotting_Markov(60) to plot at each hour (every 60 minutes).

function Offline_Markov();

% number of bins
N_bins = 20;
% length of time vector simulation
N_sim = length(Results.Prec);

% predefinitions
Temperatures = cell(1,N_sim);
ON_OFF_state = cell(1,N_sim);
vvv = cell(1,N_sim);
www = cell(1,N_sim);

%% Core loop(s)
for ii = 1:N_sim

% 1.1) get the temperature evolution at the disk of the sensor
%      for each agent
Temperatures{ii} = squeeze(Results.xrec(...
Params.sensor1_location,ii,:));

% 1.2) ON/OFF state of the internal switch for an entire day
%      for each agent
ON_OFF_state{ii} = Results.urec(1,ii,:);

% 1.3) iterate through all agents of the population
%
vvv{ii} = [];
www{ii} = [];
for jj = 1:n_app
% for each agent get the limits for the bins of the deadband

% bins lower edge
% bins upper edge
% bins center
bin_centers = (lower_limits + upper_limits)./2;

% assign values to bins
[~,binIdx] = histc(Temperatures{ii}(jj),[lower_limits upper_limits(end)]);
% store the temperature value
vvv{ii} = [vvv{ii},binIdx];
% store also if the given indeces are ON/OFF
www{ii} = [www{ii},ON_OFF_state{ii}(jj)];
end

disp([' %%%% RUN NUMBER : ',num2str(ii),' %%%%']);

end

% 1.4) convert to matrices
ON_OFF_mat = cell2mat(ON_OFF_state.');
vvv_mat = cell2mat(vvv.');
www_mat = cell2mat(www.');

% get the sub-populations for ON and OFF matrices
njs_ON = cell(1,N_sim);
njs_OFF = cell(1,N_sim);
for ii = 1:N_sim
njs_ON{ii} = vvv_mat(ii,www_mat(ii,:) == 1);
njs_OFF{ii} = vvv_mat(ii,www_mat(ii,:) == 0);
end

% 1.5) save the data
%
save('my_file_solution_bins.mat');

fprintf(' => Script terminated!\n');

end


function Plotting_Markov(intervals);

%% Do the plotting of the figures
%
% choose every how many minutes we would like to plot the Markov state
% (if not chosen take 30 minutes)
if(intervals ~= 0)
intervals = 6*intervals;
else
intervals = 6*30;
end

count = 1;
for ii = 1:N_sim
% generate a figure every 30 minutes
if(mod(ii,intervals) == 1)

temp_N_ON = sum(Results_comparison.urec(1,ii,:));
temp_N_OFF = n_app - temp_N_ON;

figure;
subplot(1,2,1);
hist(njs_ON{ii},N_bins);
ylabel('Number of ON EWHs in the bin','FontSize',12);
title(['Time step = ',num2str(ii),'; Hour = ',...
num2str(ii/360)],'FontSize',12);
h_legend = legend(['N_{ON} = ',num2str(temp_N_ON)],...
'Location','Best');
set(h_legend,'FontSize',12);
xlim([0,N_bins + 1]);
grid on;

subplot(1,2,2);
hist(njs_OFF{ii},N_bins);
ylabel('Number of OFF EWHs in the bin','FontSize',12);
h_legend = legend(['N_{OFF} = ',num2str(temp_N_OFF)],...
'Location','Best');
set(h_legend,'FontSize',12);
xlim([0,N_bins + 1]);
grid on;

% save the figure
saveas(gcf,['my_fig',num2str(count)],'png');
count = count + 1;
end
end

fprintf(' => Script terminated!\n');

end

• 1) Not relevant, but personally if this is a lab work, I think using preset (static, not changing from day to day) bin-sizes would be better to follow the progress and report the data. I see that currently it is defined by the data. 2) turning the display off before plotting something, and turning it on, after plotting something, improves performance remarkably. – Gürkan Çetin Jul 15 '15 at 20:17