function mlintro(action)
% MATLAB introductory demo

%    Yama Habibzai
%    Copyright (c) 1984-98 by The MathWorks, Inc. All Rights Reserved
%    $Revision: 1.15 $
%    $Date: 1997/12/03 17:29:57 $

if nargin<1,
  figs = allchild(0);
  figName = findobj(figs, 'Name', 'Introduction to MATLAB');
  if isempty(figName)
    action = 'init';
  else
    action ='';
  end
end

switch action
case 'init',  
  figs = allchild(0);
  figName = findobj(figs, 'Name', 'Introduction to MATLAB'); 
  if ~isempty(figName)
    clf;
  end    
  figs = allchild(0);
  fig = findobj(figs, 'Tag', 'FrontImage');
  openwin = findobj(figs, 'Tag','openwindow');
  if ~isempty(openwin)
    close(openwin);
  end
  %Reinitialize any existing buttons
  nextbtn = findobj(fig, 'Tag', 'contbutton');
  if ~isempty(nextbtn)
    set(nextbtn,'Enable', 'on', 'callback', 'mlintro func2');
  end
  
  prevbtn = findobj(fig, 'Tag', 'prevbutton');
  if ~isempty(prevbtn)
    set(prevbtn, 'Enable', 'off');
  end
  
  reset = findobj(fig, 'Tag', 'reset');
  if ~isempty(reset)
    set(reset, 'Enable', 'off')
  end
  
  clsbutton = findobj(fig, 'Tag', 'close'); 
  if isempty(fig)
    figpos = getfgpos(600, 420);
    figpos(1) = 50;
    fig = figure('NumberTitle', 'off',...
        'Position', figpos,...
        'Color', [.2 .3 .2],...
        'Tag', 'FrontImage',...
        'Resize', 'off',...
        'Units', 'Normalized',...
        'Name', 'Introduction to MATLAB');
  end
  figs = allchild(0);
  fig = findobj(figs, 'Tag', 'FrontImage');
  %----------
  labelColor=[0.8 0.8 0.8];
  yInitPos=0.90;
  top=0.95;
  btnWid=0.15;
  spacing = .03;
  btnHt=0.05;
  bottom=0.05;
  Ytopbotton = .85;
  Ysecbotton = Ytopbotton - (spacing+btnHt);
  Ythirdbotton = Ysecbotton  - (spacing+btnHt);
  Yforthbutton = Ythirdbotton  - (spacing+btnHt);
  left=0.825;
  %    % Spacing between the button and the next command's label
  spacing=0.04;
  set(gca, 'Position', [0.05 0.05 .75 .9],...
      'Tag', 'BigAxes'); 
  
  str = {' '
  ' Welcome to MATLAB. In the next few minutes',
  ' we hope to show you some of the power and'
  ' flexibility offered by the MATLAB environment.'
  ' ',
  ' This entire demonstration was written in', 
  ' the MATLAB language. The user interfaces for'
  ' all the demos were created using built-in'
  ' MATLAB screen design features.'};
  
  
  LocalSetStr(11, str);
  
  
  if isempty(nextbtn)  
    pushnext = uicontrol(fig, 'Style', 'Push',...
        'Units', 'Normalized',...
        'Position', [left Ytopbotton btnWid btnHt],...
        'String', 'Next >>>',....
        'Tag', 'contbutton',...
        'callback', 'mlintro func2');
  end
  if isempty(prevbtn); 
    pushprev = uicontrol(fig, 'Style', 'Push',...
        'Units', 'Normalized',...
        'Position', [left Ysecbotton btnWid btnHt],...
        'String', '<<< Back',....
        'Tag', 'prevbutton',...
        'Enable', 'off', 'callback', ' ');
  end
  if isempty(reset) 
    pushreset = uicontrol(fig, 'Style', 'Push',...
        'Units', 'Normalized',...
        'Position', [left Ythirdbotton btnWid btnHt],...
        'Tag', 'reset',...
        'Enable', 'off',...
        'String', 'Reset');
  end
  if isempty(clsbutton) 
    pushclose = uicontrol(fig, 'Style', 'Push',...
        'Units', 'Normalized',...
        'Position', [left Yforthbutton btnWid btnHt],...
        'Tag', 'close',...
        'String', 'Exit',...
        'callback', 'mlintro LocalClose');
    
    % Now the little membrane
    axes('Position', [.002 .855 .11 .15]);
    set(gca, 'Tag', 'LittleAxes');
    logo5;
    
  end
otherwise,
  switch action
  case 'func2', func2;
  case 'func3', func3;
  case 'func4', func4;
  case 'func5', func5;
  case 'func6', func6;
  case 'func7', func7;
  case 'func8', func8;
  case 'func9', func9;
  case 'func10', func10;
  case 'LocalClose', LocalClose;
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Local Functions (SLIDES) %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%% SLIDE 2 %%%%%%%%%%%%%%%%%%%%
function func2
figs = allchild(0);

introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro init');
set(nextbutton, 'callback', 'mlintro func3');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');

str ={' '
' MATLAB excels in the area of matrix'
' computation.'
'                                                            '
' You can create and load a variety'
' of data types, such as matrices, data'
' arrays, structures, character arrays,'
' and cell arrays, into MATLAB.'
' '
' The data can then be analyzed and'
' visualized in numerous ways.'};

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
   if ~isempty(openwin)
     close(openwin);
   end
   
%Now put the little membrane on top
LocalPutMem;

%%%%%%%%% SLIDE 3 %%%%%%%%%%%%%%%%%%%%
function func3
figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func2');
set(nextbutton, 'callback', 'mlintro func4');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');

str ={' '
' The following code shows a simple example'
' using the MATLAB language. First, we load'
' a data file  -- durer --  into MATLAB. Then'
' we display the data with the image function.'
' '
'>>load durer;'
'>>image(X);'
'>>colormap(map);'
' ' 
'The image is Albrecht Durer''''s Melancolia.'
' '
'Can you find the magic square in the image?'
' '
'hint: top, right'}; 

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end


openfig = figure('Tag', 'openwindow',...
    'Position', getfgpos(350, 350));


set(introfig, 'Pointer','watch');
load durer;
image(X);colormap(map);
set(gca, 'Position', [0 0 1 1]);
axis off;
set(introfig, 'Pointer', 'arrow');
figure(openfig);
%Now put the little membrane on top
LocalPutMem;
%Make the graphic window appear in top;
figure(openfig);
%Make the graphic window appear in top;
figure(openfig);

%%%%%%%%% SLIDE 4 %%%%%%%%%%%%%%%%%%%%
function func4
figs = allchild(0);

introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func3');
set(nextbutton, 'callback', 'mlintro func5');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');
 
str ={' '
' Here, we load another file which contains'
' the data for the magic square portion of'
' the image.'
' '
'>>load detail;'
'>>image(X);'
'>>colormap(map);'
' '
' A matrix is considered magic when'
' the sum of every row, column, and'
' diagonal are equal. Try typing'
' '
' magic(5)'
' '
' on the MATLAB command line.'
' '};

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end

figpos = getfgpos(350, 350);
openfig = figure('Tag', 'openwindow',...
    'Position', figpos);

load detail;
image(X);colormap(map);
set(gca, 'Position', [0 0 1 1]);
axis off;

%Now put the little membrane on top
LocalPutMem;
%Make the graphic window appear in top;
figure(openfig);

%%%%%%%%% SLIDE 5 %%%%%%%%%%%%%%%%%%%%
function func5
figs = allchild(0);
 
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func4');
set(nextbutton, 'callback', 'mlintro func6');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');
 
str ={' '
' MATLAB has functions for nearly every'
' type of matrix calculation.'
' For example:'
' '
' - Eigenvalues'
' - Inverse of a matrix'
' - Singular value decomposition'
' - Roots of a function' 
' - Sparse matrices'
' - LU factorization'
' '
'   and many more.'}; 

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end

%Now put the little membrane on top
LocalPutMem;

%%%%%%%%% SLIDE 6 %%%%%%%%%%%%%%%%%%%%
function func6
figs = allchild(0);

introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func5');
set(nextbutton, 'callback', 'mlintro func7');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');

str ={' '
' MATLAB also allows you to create specialized'
' engineering plots. Here is an example of a'
' spline curve fit to an 8 point data series.'
' '
' In addition to the standard 2-D and 3-D plots,'
' a subset of the MATLAB plotting types includes'
' ' 
' - polar plots'
' - log and semilog plots'
' - 2-D and 3-D bar graphs and pie charts'
' - feather and quiver plots'
' - histograms'
' - stem plots'
' - stairstep plots'
'   and more.'};


LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end


openfig = figure('Tag', 'openwindow',...
    'Position', getfgpos(424, 355));

a=[1 4 3 8 5 9 4 6];
t=0:7;
t2=0:.1:7;
a2=spline(t,a,t2);
hold on;
plot(t2,a2);
plot(t,a,'c*');
title('Spline Curve Fitted to Data Series');

%Now put the little membrane on top
LocalPutMem;
%Make the graphic window appear in top;
figure(openfig);

%%%%%%%%% SLIDE 7 %%%%%%%%%%%%%%%%%%%%
function func7
figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func6');
set(nextbutton, 'callback', 'mlintro func8');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');

str ={' '
' MATLAB provides fast and accurate'
' graphics and visualization capabilities.' 
' '
' Here, we display the spherical harmonics,'
' the spherical version of Fourier series.'
' They can be used to model the free' 
' oscillations of the Earth.'
' '
' Try using the mouse to rotate the image.'}; 

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end


openfig = figure('Tag', 'openwindow',...
    'Position', getfgpos(350, 350));
set(introfig, 'Pointer','watch');
n = 6;                          % degree = 6
m = 1;                          % order = 1
theta = 0 : pi/40 : pi;         % altitude
phi = 0 : pi/20 : 2*pi;         % azimuth
[phi,theta] = meshgrid(phi,theta);

Ymn = legendre(n,cos(theta(:,1)));
Ymn = Ymn(m+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
  yy = [yy Ymn];
end;
yy = yy.*cos(m*phi);

m = max(max(abs(yy)));
rho = 5 + 2*yy/m;

% spherical coordinate equations
r = rho.*sin(theta);
x = r.*cos(phi);    % spherical coordinate equations
y = r.*sin(phi);
z = rho.*cos(theta);
 
% do the plot
s = surf(x,y,z);
axis('square')
set(s, 'clipping', 'off');
axis([-6 6 -6 6 -6 6])
axis('off')
view(40,30)
light;
lighting phong;
axis vis3d;
axis square;
rotate3d;
set(gcf, 'color', [1 1 1]);
set(introfig, 'Pointer', 'arrow');

%Now put the little membrane on top
LocalPutMem;
%Make the graphic window appear in top;
figure(openfig);

%%%%%%%%% SLIDE 8 %%%%%%%%%%%%%%%%%%%% 
function func8
figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func7');
set(nextbutton, 'Enable', 'on','callback', 'mlintro func9');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');
 
str ={' '
' MATLAB''''s graphical capabilties allow'
' you to create:'
' '
' - Triangular mesh and surface plots'
' - Animations'
' - Patch, cylinder, and spherical solid models'
' - Graphical user interfaces (GUIs)'
' '
' and to control such properties as:'
' ' 
' - Lighting and shading'
' - Material reflectance'
' - Camera view angle, target, and position'
' '
'   and more.'};

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end

%Now put the little membrane on top
LocalPutMem;

%%%%%%%%% SLIDE 9 %%%%%%%%%%%%%%%%%%%%
function func9
figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func8');
set(nextbutton, 'Enable', 'on','callback', 'mlintro func10');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');

str ={' '
' Here, MATLAB''''s Delaunay triangulation, data'
' gridding, and surface fitting functions'
' display raw data from an underwater mountain'
' as a 2-D and then a 3-D image.'
' '
' The image is then lit, colored, and shaded with'
' MATLAB''''s visualization functions.' 
' '}; 

LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end


openfig = figure('Tag', 'openwindow',...
    'Position', getfgpos(395, 326));

set(introfig, 'Pointer','watch');
load seamount;
axes('Position', [.1 .67 .3 .3]);
plot(x,y,'.','markersize',6);
xlabel('Longitude'), ylabel('Latitude');
tri = delaunay(x,y);
size(tri);
% Plot triangles
hold on, trimesh(tri,x,y,z), hold off;
set(gca, 'Xlim', [210.8 211.75], 'Ylim', [-48.5 -47.9]);
%-------------------------------------------------
% Plot lighted surface
axes('Position', [.3 .3 .37 .37]);
trisurf(tri,x,y,z);
xlabel('Longitude'), ylabel('Latitude'), zlabel('Depth');
view(-37,20);
set(gca,'dataaspectratio',[1/74300 1/111000 0.2]);
axis([min(x) max(x) min(y) max(y) min(z) max(z)]);
set(gca, 'XLim', [210.75 211.5], 'Ylim', [-48.5 -48.1]);
%-------------------------------------------------
axes('Position', [.6 .01 .37 .37]);
trisurf(tri,x,y,z);
view(-37,20);
set(gca,'dataaspectratio',[1/74300 1/111000 0.2]);
axis([min(x) max(x) min(y) max(y) min(z) max(z)])
set(gca, 'XLim', [210.75 211.5], 'Ylim', [-48.5 -48.1]);
 
colormap(jet(64)), shading interp;
light('Position',[1 .1 1.2].*get(gca,'dataaspectratio'));
lighting gouraud;
set(gca, 'XtickLabel', []);
set(gca, 'YtickLabel', []);
set(gca, 'ZtickLabel', []);
set(gcf, 'Color', [1 1 1]);
set(introfig, 'Pointer', 'arrow');

%Now put the little membrane on top
LocalPutMem;
%Make the graphic window appear in top;
figure(openfig);

%%%%%%%%% SLIDE 10 %%%%%%%%%%%%%%%%%%%%
function func10
figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
nextbutton = findobj(introfig, 'Tag', 'contbutton');
prevbutton = findobj(introfig, 'Tag', 'prevbutton');
resetbutton = findobj(introfig, 'Tag', 'reset');
set(prevbutton, 'Enable', 'On','callback', 'mlintro func9');
set(nextbutton, 'Enable', 'off');
set(resetbutton, 'Enable', 'on', 'Callback','mlintro init');
 
str ={' '
' MATLAB''''s power does not end here.'
' '
' Use the MATLAB Tour interface to access'
' numerous other application-specific demos'
' and examples.'
' ' 
' '
' We hope you enjoy these demos.'
' '}; 
LocalSetStr(11, str);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end

%Now put the little membrane on top
LocalPutMem;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Function to set Text %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalSetStr(fsize, str)

figs = allchild(0);
introfig = findobj(figs, 'Tag', 'FrontImage');
ax = findobj(introfig, 'Tag', 'BigAxes');

axes(ax);
cla reset

set(ax,'Box','off','XTick',[], 'Tag', 'BigAxes','YTick',[],'Visible','on');

siz = size(str);
for i = 1:siz(1)
  eval(['text(0.06,' num2str(1-0.06*i) ', ' ''''str{i}'''', '''', ', ''FontName''',', ' '''helvetica''', ', ' '''fontweight''', ', ' '''bold''', ', ' '''fontsize''', ', ' ''num2str(fsize)'' ');']);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalClose %%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalClose 

figs = allchild(0);
%close any open demo window
openwin = findobj(figs, 'Tag','openwindow');
if ~isempty(openwin)
  close(openwin);
end
close;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalPutMem %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalPutMem()
% Put little membrane on top

allfigs = allchild(0);
introfig = findobj(allfigs, 'Name', 'Introduction to MATLAB');
littleaxes = findobj(introfig, 'Tag', 'LittleAxes');
axes(littleaxes);
 

