%% sl 6: Hello World example
thisTimer = timer;
thisTimer.Period         = 1/2;
thisTimer.TasksToExecute = 15;
thisTimer.ExecutionMode  = 'fixedRate';
thisTimer.TimerFcn       = @timer_update;
start(thisTimer);

function timer_update(myTimer, ~)
    fprintf(1, '%02d | Hello world!\n', myTimer.TasksExecuted);
end

%% sl 7+8: Capture point Location
axSize = [500 400];
hFig = uifigure('color', 'white', ...
'pos', [50 50 axSize]);
hAxes = uiaxes('parent', hFig, ...
'pos', [0 0 axSize], 'box', 'on', ...
'XTickLabel', '', 'YTickLabel', '');
hAxes.XLim = [0 1];
hAxes.YLim = [0 1];

tT = timer;
tT.Period = 0.5;
tT.ExecutionMode = 'fixedRate';
tT.TimerFcn = @(src, event) timer_update2(hAxes, groot);
hFig.CloseRequestFcn = @(a,b)closeReq(hFig,tT);
drawnow;
start(tT)
function timer_update2(hAx, grt)
    pos = grt.PointerLocation ./ grt.ScreenSize(3:4);
    line(hAx, [pos(1) pos(1)], [pos(2) pos(2)], ...
    'LineStyle', 'none', 'Marker', '.', 'MarkerSize', 20, 'Color', 'r');
end
function closeReq(hFig, tT)
    stop(tT);
    delete(hFig);
end

%% sl 14: 10k day party
Days = {'Sun', 'Mon', 'Tue', 'Wen', 'Thu', 'Fri', 'Sat'};
birth = datenum(1985, 03, 26);
delay = days(1e4);
day1e4 = datestr(birth + delay)
weekDay = Days{weekday( day1e4)}

%% sl 23: Bubble sort algorithm
clear; close all; clc
N = 30;
R = 10*rand(1, N);
for startMove = 1:N-1
    for pos = 1:N-startMove
        if R(pos) > R(pos+1)
            R([pos, pos+1]) = R([pos+1, pos]);
        end
        figure(1);
        plot(R,'*','LineWidth',2);
        pause(0.01);
    end
end
issorted(R)

%% sl 25: Shaker sort algorithm
R = 10*rand(1, 30);
figure(1);
for startMove = (length(R)-1):-1:length(R)/2
    for pos = startMove:-1:(length(R)-startMove)
        if R(pos) > R(pos+1)
            R([pos, pos+1]) = [R(pos+1) R(pos)];
        end
    end
    for pos = (length(R)-startMove+1):startMove
        if R(pos) > R(pos+1)
            R([pos+1, pos]) = [R(pos), R(pos+1)];
        end
    end
    plot(R, '*', 'LineWidth', 2);
    pause(0.15);
end

%% sl 27: array searching I
v = reshape(magic(4)', [1 numel(magic(4))])
v = sort(v, 'descend');
v1 = mod(v, 3) == 0 & v > 10
v(v1)
find(v1)

%% sl 28: array searching II
w50 = w(w < 50);
w50(end-2:end)
% w50 = [20; 12; 31.5]
[rw, cl] = find(w < 50, 3, 'last');

%% sl 29: Decrypting a message
chars = 'acdgmrs'; volts = 1:7;
message = blanks(length(samples));
for iVolt = volts
   logCondition = samples > (iVolt - 0.5) & ...
      samples < (iVolt + 0.5);
   indices = find(logCondition);
   message(indices) = chars(iVolt);
end
disp(message)

%% sl 33: accumarray I
dta = [1 -110; 1 -140; 2 -22; 3 -2; ...
2 -34; 1 -1300; 2 -15; 1 -730; 3 24]
K = [1 26 25]
M = accumarray(dta(:, 1), dta(:, 2))
acc = K*M

%%Exercise I: Measured data
data = accumarray(TimeAndPlace, MeasuredData, [], @mean);
dataA = data(:,1); dataB = data(:,2); dataC = data(:,3);