MATLAB tricks (L.K.)
Link to my MATLAB directory ....
Listings of demo files of MATLAB ....
Dirac Notation Interpreter
On Line Matlab ....
%% Plotting the 3-dim FYS208
%% Brillouin, 2dim crystal, tight binding
% can be run by Matlab< fff.html
T41=41;
T20=20;
for i=1:T41
x(i)=-pi+ pi/T20*(i-1);
end
for i=1:T41
y(i)=-pi+ pi/T20*(i-1);
end
for i=1:T41
for j=1:T41
f(i,j)=3.0-cos(x(i))-cos(y(j));
end
end
u(:)=3.0-cos(x(:));
%% Mesh surface:
surfl(f)
%% Add shading
shading interp
colormap(gray);
%% Plot in grey and add contours.... This is good !
surfc(f)
shading interp
%% Postscript
print -deps Plot1.eps
%% Aspect Ratio (get current ... gca )
set(gca,'AspectRatio',[1 1])
print -deps Plot2.eps
%% contour
contour(f,12)
set(gca,'AspectRatio',[1 1])
print -deps Plot2.eps
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% To obtain a special aspect ratio on*.eps
%%
%% use
%%
set(gcf,'PaperPosition',[1 1 8 3])
%%
%% where the last 2 numbers make it !
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% contour with labels
c= contour(f,12);
clabel(c);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% controls
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Initialize f1(x), f2(x), x, a.
if ~exist('f1'), f1 = 'x^2'; end
if ~exist('x'), x = '[-2*pi, 2*pi]'; end
if ~exist('a'), a = '1/2'; end
if ~isstr(a), a = sym(a); end
% Position three figures.
delete(get(0,'children'))
set(0,'DefaultUicontrolUnits','normalized')
figure('units','normalized','pos',[.01 .50 .48 .48])
figure('units','normalized','pos',[.50 .50 .48 .48])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simplest slider
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
set (1,'name','kontrol')
F1 = uicontrol('style','slider','call','a=get(F1,''Value'')')
% Outputs a every time slider moved...
%
% To stop it:
%
delete(get(1,'children'))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Editable text.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
blanks = ' ';
figure(2)
axes('pos',[0 0 1 1]); axis off
ConPos1 = [ 0.2 0.65 0.8 0.07];
F1 = uicontrol('pos',ConPos1 ,'style','edit','horiz','left', ...
'string', [blanks f1],'call','f1 = get(F1,''string''); ezplot(f1,x,1)');
% POS: xpos ypos xsize ysize
imtext(0.03,0.68,'f1 = ','left');
imtext(.07,.91,'CONTROL FOR PLOTTING - Enter function ','left');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT and eval(string)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% statement in your program
buuu=input('......','s')
%
% next statement in your program
%
eval(buuu)
%
% showing that you can input an expression OVER defined variables
%
zz=5
fprintf(1,'\nenter f.ex. zz^2\n')
aaaa=input(' . . . . aaaa ?? ')
%
aaaa=input(' . . . . aaaa ?? '); % does not echo
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Graphical Input example
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
u=[0 5];
v=u;
% u and v define the scale
x=[0 0 0 0 0 0 0 0 0 0 ];
y=x;
plot(u,v,'.',x,y,'-')
%
% now we have a plot to use for graphinput
%
[x,y]=ginput(15);
plot(u,v,'.',x,y,'-')
%
% here we use varying figures
%
figure
figure(2)
plot(u,v,'.',x,y,'-')
figure(1)
[x,y]=ginput(15);
figure(2)
plot(u,v,'.',x,y,'-')
figure(1)
plot(u,v,'.',x,y,'-')
%
% end Graphical input
%
% write data
%
fid = fopen('ans1.txt','w');
fprintf(fid,'%6.2f \n',ans);
%
%
%
% Maple functions
%
maple('diff(x^2,x)')
mhelp('diff')
maple('diff(1/(x+2),x);')
%
%
%
% Opens three windows
% Executes button functions in each window
%
%
% Position three figures.
%
delete(get(0,'children'))
set(0,'DefaultUicontrolUnits','normalized')
figure('units','normalized','pos',[.01 .50 .48 .48])
figure('units','normalized','pos',[.50 .50 .48 .48])
s = get(0,'screensize');
if s(3) > 750
figure('units','normalized','pos',[.25 .01 .50 .44])
else
figure('units','normalized','pos',[.10 .01 .80 .44])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('x'), x = '[-2*pi, 2*pi]'; end
f1 = '1/(5+4*cos(x))'
f2 = '(exp(x/pi)*4*cos(x))'
f3 = '(exp(-x/pi)*4*sin(x))'
figure(1)
T1 = uicontrol('pos',[.03 .03 .07 .07],'string','f1','Callback',...
'ff1');
figure(2)
T1 = uicontrol('pos',[.03 .03 .07 .07],'string','f2','Callback',...
'ff2');
figure(3)
T3 = uicontrol('pos',[.03 .03 .07 .07],'string','f3','Callback','ff3');
%
% ff3.m follows
%
figure(3);
ezplot(f3,x);
%
% end ff3.m
%
% Example editable text
delete(1)
figure(1)
%% define position and dimensions
xh=10;yh=hig+2;
wid=100;hig=28;
%% Lower control text
ht=uicontrol('Style','text',...
'Position',[xh yh wid hig],'String','Enter a');
%% Lower control edit
h = uicontrol('Style','edit','Position',...
[xh yh-hig-2 wid hig],...
'Callback','a=str2num(get(h,''String'')),set(h,''String'',num2str(a));',...
'String',' ');
%% Upper control text
gt=uicontrol('Style','text',...
'Position',[xh yh+2*hig+10 wid hig],'String','Enter b');
%% Upper control edit
g= uicontrol('Style','edit','Position',...
[xh yh+hig+8 wid hig],...
'Callback','b=str2num(get(g,''String'')),set(g,''String'',num2str(b));',...
'String',' ');