%
% Application Programming in MatLab-environment
% Autumn 2000
% 
% Exercise 3 & 4
%

% Clear the variables and close all windows
clear
close all

% ----- Problem 1 -----
% a) operations from the command-line using get/set functions

x = 0:0.1:3;
y = x;
f = plot(x,y);

disp('Properties of the current figure:')
get(gcf)

% List of possible values for the properties can be obtained by
% set(gcf).

% Alter the background color.
% Color is determined by Color-property the value of which is
% a vector with three real numbers. They correspond to the 
% RGB-values of the color.
set(gcf,'Color',[1 0 0]);

disp('Press any key.'); pause
set(gcf,'Color',[1 1 1]);

% Shift the figure window to the upper left corner of the display.
% The location of the figure is given by the Position-property the 
% value of which is a vector with four elements.
% The first two give the coordinates of the lower left corner of the
% window on the display (root). The origin of the display is on the 
% lower left corner. The next two values give the width and height
% of the window in pixels.
%
% Attention. The location depends on the resolution of the display.
% Handle to the root is given by the Parent-property of the
% current figure. The properties of the root, such as ScreenSize,
% can then be checked with get-function.
r  = get(gcf,'Parent')
rs = get(r,'ScreenSize')
fs = get(gcf,'Position')
set(gcf,'Position',[1 rs(4)-fs(4) fs(3) fs(4)])

% Change the thickness and color of axes.
% They are determined by the properties LineWidth, XColor, and YColor
%
% Attention. Color determines the background color of axes
set(gca,'LineWidth',2)
set(gca,'XColor',[1 0 0])
set(gca,'YColor',[1 0 0])

% Change the graph to the interval [1,4] without recreating
% the figure. It can be done by changing the properties XData
% and YData.
disp('Current properties of the figure:'); get(f)
disp('Press any key.'); pause

x = 1:0.1:4; y=x;
set(f,'XData',x,'YData',y)

% Add markers to the graph and change its color.
% In addition to the Marker-property it is possible to 
% set MarkerSize, MarkerEdgeColor and MarkerFaceColor. 
% set(f) gives the possible settings of Marker.
set(f,'Marker','square')
set(f,'Color',[1 0 0])

disp('Press any key.'); pause
close
clear all


% ----- Problem 3 -----
% Define grid and compute values on the grid.
[x y] = meshgrid(-0.5:0.01:0.5);
z = sin(4*pi .* x) .* sin(4*pi .* y);

% 1st way
disp('3D colored mesh')
mesh(x,y,z)
%meshz(x,y,z)

disp('Press any key.'); pause

% 2nd way
disp('3D colored surface')
figure
surf(x,y,z)
%surfl(x,y,z)

disp('Press any key.'); pause

% 3rd way
disp('Contour plot')
figure
contour(x,y,z)
%contour3(x,y,z)

disp('Press any key.'); pause

% 4th way
disp('Filled contour plot')
figure
contourf(x,y,z)

disp('Press any key.'); pause

% 5th way
disp('Combined 3D surface and contour plot')
figure
surfc(x,y,z)
%meshc(x,y,z)

disp('Press any key.'); pause

% 6th way
disp('Waterfall')
figure
waterfall(x,y,z)

disp('Press any key.'); pause

% 7th way
disp('Digital image based on function values')
figure
image(z,'CDataMapping','scaled')

disp('Press any key.'); pause
close all
clear all

% ----- Problem 4 -----
% Create a random NxM matrix
N = 5; M = 6;
A = randn(N,M);

% Try plot-function
% Result is a set of graphs of columns versus row-indices
plot(A)

disp('Press any key.'); pause
close all

% 1st alternative, bar-plot
% Gives N groups of M bars. The height of the bar corresponds to the
% value of the matrix entry.
bar(A)
title('Matrix rows')

% Same for the transpose
figure; bar(A')
title('Matrix columns')

disp('Press any key.'); pause
close all

% 2nd alternative
% illustrate rows and columns in separate graphs
figure;
rc = ceil(sqrt(N));
rf = floor(sqrt(N));
for i=1:N,
   subplot(rc,rf,i), plot(1:M,A(i,:));
   xlabel('column index');
   ylabel('matrix entry');
   set(gca,'XTick',1:M);
end

disp('Press any key.'); pause

figure;
sc = ceil(sqrt(M));
sf = floor(sqrt(M));
for i=1:M,
   subplot(sc,sf,i), plot(1:N,A(:,i));
   xlabel('row index');
   ylabel('matrix entry');
   set(gca,'XTick',1:N);
end

disp('Press any key.'); pause
close all
clear all

return

