%% Introduction to MATLAB 

%When MATLAB is started it runs the script 'startup.m' located in
%Documents\MATLAB\ (windows)

%Type into Command Window
2+2

% Trick- select+F9 runs the selected text on the command line

%assign a value to a variable
a=2

%see the variable 
a

%list all variables
whos

%mathematical functions can be performed with variables
a=2
b=3
c=a*b

a*b %result not assigned to variable. 

%clear all variables
clear

whos

a=1; %the semicolon suppresses the output. 
%but a exists
a
whos

%get help on a function
help clear

%get even more help 
doc clear

%MATLAB documentation is very good.  Once you get started you can learn
%most of what you need to know from the documentation.  


%% Scripts
% Scripts Can be used to keep track of a sequence of MATLAB commands
% This is a script!
%Scripts and functions are text files with the extension *.m

%We can run a script from a script
%the commands in the script are run on the command line

ExScript1

%Warning! A script can overwrite your variables or clear them. Avoid
%scripts when possible and use functions.

a=1

ExScript1
a

%% Functions

%Functions can be m-files, inline functions or compiled functions. Lets
%stick to the first two for now.

a=4
in1=2
in2=3

help ExFunc1

%call the function
[o1 o2]=ExFunc1(in1,in2)

a

%Note 'a' is not modified.  What happens in a function stays in a function
%(except for its outputs)

% For small things, sometime an inline function is easier

%doc inline

ExInF=inline('x+y','x','y')

o=ExInF(in1,in2)

%% Loops, conditional statements, and logical statements

%print all odd numbers from 1 to 10

for ii=1:10
    if ii/2~=round(ii/2)
        ii
    end
end

%print all even numbers from 1 to 10 (but prettier)

for ii=1:10
    if ii/2==round(ii/2)
        fprintf('Found an even: %d \n',ii)
    end
end

%% Arrays and Matricies

a=(0:.1:10)
b=2*a'    %the ' means transpose

size(a)
size(b)

a*a     %This doesn't work since MATLAB wants to do a matrix multiply

a.*a    %This gives an element by element operation

a*b     %This does work, but gives inner product (is that what you want?)

%Get the first element of 'b'
b(1)

%Change the first element of 'b'
b(1)=10;

%Get the first element of 'b'
b(1)

%Get the first 10 elements of 'b'
b(1:10)

%put the first 10 elements of 'a' into 1st 10 of 'b'

b(1:10)=a(1:10);
b(1:10)

c=[1 2; 3 4]

%Get first column
c(:,1)  %here, the ':' means all elements in that dimension

%Get first row
c(1,:)

%% Plotting 

x=(0:.1:10)';
y=sin(x)+1;

figure  %This opens a new, blank figure 
plot(x,y)

%Now make the PI happy with some labels
xlabel('Time after buffer exchange (s)','fontsize',14)
ylabel('Normalized Response (AU)','fontsize',14)
legend('Calcium Signal')
title('Type 5 Rat Cells','fontsize',14)




%plot two things on one graph
x=(0:.1:10)';
y=sin(x)+1;
y2=1+exp(-x);

figure  %This opens a new, blank figure 
plot(x,y,'r','linewidth',2)
hold on     %without this, new plots overwrite old plots
plot(x,y2,'g--','linewidth',2)
xlabel('Time after buffer exchange (s)','fontsize',14)
ylabel('Normalized Response (AU)','fontsize',14)
legend('Calcium Signal','Cell Population')
title('Type 5 Rat Cells','fontsize',14)


%% DIPimage

%Install DIPimage, see example startup function

%bring up the menu
dipimage

%use 'readim' to load an image.

a

%
a = readim('erika.ics','')

%can convert MATLAB <-> DIPimage
%This conversion Transposes 1st two dimensions!

b=double(a)

%DIPimage objects have 0-based indexing

%get top half of image
c=a(:,0:127)

%find brightest regions
c=a>128

%get average value
help mean   %Note overloaded function for DIPimage

mean(a)

%Get mean value of brightest pixels
mean(a,c)

%Get mean value of brightest pixels in each column
mean(a,c,2)

%Now lets try using 'measure'

%make a fake data set
a=newim
b=noise(a)
c=b>3
d=1000*gaussf(c,2.5)
data=noise(d,'poisson')

%smooth our data
gf=gaussf(data,1)

%threshold
mask=gf>1

%label the individual spots
li=label(mask)

%run from menu then copy the command window text.
msr = measure(li,data,{'Size'},[],Inf,0,0)

sz=msr.Size

%Do not overwrite matlab function 'size'!

%plot a histogram of the blob sizes
figure
hist(sz,10)
xlabel('Blob Size (pixels)','fontsize',14)
ylabel('Counts','fontsize',14)


%3D image
ex=noise(newim([128 128 50]))
% 'n' and 'p' for 'next' and 'previous'




 

