%% Hello World example
lastSeq   = 15;
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

%% Timer - extra example
tT = timer;
tT.Period        = 0.5;
tT.ExecutionMode = 'fixedRate';
tT.TimerFcn      = @(src, event) timer_update(hAxes, groot);
hFig.CloseRequestFcn =  @(a,b)closeReq(hFig,tT);

drawnow;
start(tT)

function timer_update(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

%% 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)

% For bubble sort (hint)
        figure(1);
        plot(R,'*','LineWidth',2);
        pause(0.01);
        
%% Day of life, day of the week
clear; clc;
Days    = {'Sun', 'Mon', 'Tue', 'Wen', 'Thu', 'Fri', 'Sat'};
birth   = datenum(1985, 03, 26);
delay   = days(1e4);
day1e4  = datestr(birth + delay)
weekDay = Days{weekday( day1e4)}

%% Bubble sort (bar)
clear; close all; clc;
R = 10*rand(1,20);
figure(1);
for startMove = (length(R)-1):-1:1
   for pos = startMove:(length(R)-1)
      if R(pos) > R(pos+1)
         R([pos, pos+1]) = [R(pos+1) R(pos)];
         bar(R);
         hold on;
         bar(pos+1, R(pos+1), 'g');
         hold off;
         pause(0.01);
      else
         bar(R);
      end
   end
end
issorted(R)

%% Shaker sort
clear; close all; clc;
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

%% Sorting a vector
>> v  = reshape(magic(4)', [1 numel(magic(4))])
>> v  = sort(v, 'descend');
>> v1 = mod(v, 3) == 0 & v > 10
>> v(v1)
>> find(v1)

%% Find within a vector
v  = reshape(magic(4)', [1 numel(magic(4))])
v  = sort(v, 'descend');
v1 = mod(v, 3) == 0 & v > 10
v(v1)
find(v1)

%% Find within a matrix
w50 = w(w < 50);
w50(end-2:end)
% w50 = [20; 12; 31.5]
[rw, cl] = find(w < 50, 3, 'last');

%% 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)

%% Accumarray example:
>> M   = accumarray(dta(:, 1), dta(:, 2))
>> acc =  K*M  % = -3576
>> % acc =  dot(K, M)
>> % acc =  sum(K.*M.')
>> % acc =  sum(K.'.*M)