%% For loops
2^1
2^2
2^3
2^4

% For loops allow you to run the same equation many times, but adjusting a
% value during each one.
for j = 1:4
    2^j
end

% You can assign variables during a for loop
indx = 0;
for j = 1:2:30
    Power2(j) = 2^(.5*j);
end

figure
plot(Power2)

% Note, this one could also be done in a single line.
x = 1:30;
Power2b = 2.^x; % Have to use the dot to say element wise power


%% Loading in and displaying images
clear
close all

% Move to the directory with the images
cd('C:\Users\joelkralj\Dropbox\Camera Uploads')

% Define the name of the file you want to load.
% Note, I'm putting it in single quotations to define a string
fname = '2016-08-26 12.20.08.jpg';

% Load in the image using imread
img = imread('string.jpg');
[ysize,xsize,ncolors] = size(img)

% Note, the color indices are:
% 1 - red
% 2 - green
% 3 - blue

% Display the image
figure
imshow(img)

% Show just the red channel
figure
imshow(img(:,:,1))

% Show just the blue - green channel
figure
imshow(img(:,:,3) - img(:,:,2))

%% The use of cells
clear
close all

% Cells allow different types of information to be stored while still
% indexing them.  Useful if you want to run for loops with different sizes
% of information.

myCell{1} = 55;
myCell{2} = [35 66];
myCell{3} = [1:35  42:50];
myCell{4} = 'Cells are the coolest';

% Length will give you the total number of cells in the variable
ncells = length(myCell)

% You can index cells with the () in a similar way.
myCell{1}(1) % This one is obvious
myCell{1}(2) % Throws an error, there is only 1 element
myCell{3}(1:3) % Asks for the first 3 elements in the 3rd cell
myCell{4}([1 4 8 12])

% You can use a for loop to do the same action on every cell.
for j = 1:4
    myCell2{j} = myCell{j} + 5;
end

% This can also be useful for loading in images with different names
fname2{1} = '2016-08-26 12.20.08.jpg'; % Image from my dropbox
fname2{2} = '2016-08-21 10.33.04.jpg'; % Image from my dropbox
fname2{3} = 'moon.tif'; % Stock matlab image

nfiles = length(fname2);

% Use a for loop to load all the images into 1 big variable with 3 cells
for j = 1:nfiles
    imgCell{j} = imread(fname2{j});
end

size(imgCell{1})
size(imgCell{3})

% Display the images.  Note the subplot command makes 2 axes on a single
% figure.
figure
subplot(1,2,1)
imshow(imgCell{2})
subplot(1,2,2)
imshow(imgCell{3})


%% Types of variables
clear
close all

% We know about numbers.  Easy to store them as variables, arrays, and
% matrices

a = 7
b = 55
c = 1:7


% If you just create a number, it is a double, a floating point that will
% take up 8 bytes per number
whos

% Not all numbers are created equal
u1 = uint8(9)
whos

% Uint8 -> unsigned integer.  Can go up to 2^8 = 255.  Has to be an
% integer.
u2 = u1/2

% Possible to convert back to double
u3 = double(u2)
u4 = u3/2


% We can also add characters.  These are obviously letters.  Strings are
% defined by using single apostrophe

d = 'hello world'
e = 'Quantitative optical imaging rocks'

% Character arrays are accessed exactly the same way as numerical arrays
d(1)
d(7)
e(1:7)
e([1 3 4 5 7])


% Structures are a class that can hold different types of data, with each
% one having a name

students.name = 'Johnny Appleseed'
students.age  = 24
students.grade = 'A'

students(2).name = 'Ralphie Buffalo'
students(2).age = 7
% If you look at variable editor, you can see all these inserted into a
% matrix
students(2).grade = 'C';

students.name
students.grade
