MATLAB: Inverse index problem.

indexindexinginverse matrixmatrix arrayplot

Shortly, I have a sensor that logs the data and some values. In this example I process only temperature and time. What I wanted to do is to make an index which will filter through occupancy hours, so I pick just data when I define someone is in this room. The problem for me comes when I have to select the index interval. Here is the simplification of code and I'll explain what exactly is causing trouble:
time_sensor = datenum(sensor.textdata);
temperature = sensor.data(:,3);
timevec = datevec(time_sensor);
ind_occ = timevec(:,4)>9 & timevec(:,4)<16;
ind_occ = (1 - ind_occ);
Here comes the problem; I basically want to exclude the time from 9 to 16h, therefore I made the index for that period and the idea was to just inverse that matrix with this line. If I comment it out, the plot runs with no problems. With this it returns an error and no plot: (Subscript indices must either be real positive integers or logicals. Error in example (line 34) plot(time_sensor(ind_occ,:),temperature(ind_occ,:));)
mintime=time_sensor(1,1);
maxtime=time_sensor(end,1);
figure (1)
plot(time_sensor(ind_occ,:),temperature(ind_occ,:));
axis([mintime,maxtime,12,25]);
datetick('x',1,'keeplimits','keepticks')
title(sprintf('max: %0.1f | min: %0.1f | average: %0.1f',max(temperature(ind_occ,:)),min(temperature(ind_occ,:)),mean(temperature(ind_occ,:))));
ylabel('Temperature');
Thanks for the help. I am very very new to Matlab, so sorry if I made some rookie mistake!

Best Answer

You can create the logical inverse by using a logical not ~:
ind_occ = ~ind_occ;
or
ind_occ = ~(timevec(:,4)>9 & timevec(:,4)<16);
And using this wherever you currently use ind_occ. Or alternatively you can simply invert the comparisons:
ind_occ = timevec(:,4)<= | timevec(:,4)>=16;
Note that in both cases you do not need this line, so delete it:
ind_occ = (1 - ind_occ);