% This m-file is an introduction to graphics and displaying text clear %% Displaying text disp('This is an introduction to displaying text'); disp(' '); % Display a blank line disp('Today is ...'); disp(date) time=fix(clock); % Get time as integers whos time % Structure of time matrix hourstr=int2str(time(4)); % hours as a string (i.e. text) minstr=int2str(time(5)); % hours as a string (i.e. text) if time(5)<10 minstr=['0',minstr]; % Add a leading zero if necessary end timex = [hourstr ':' minstr]; % create a time string disp(' '); disp('And the time is ...'); disp(timex); %% Input and output % Writing formatted data to a file (temperature conversion table) F=-40:5:100; C=(F-32)*5/9; t=[F;C]; % creat a 2x100 matrix fid=fopen('Temperature.table','w'); fprintf(fid,' Temperature Table\n '); fprintf(fid, 'Fahrenheit Celsius \n'); fprintf(fid,' %4i %8.2f\n',t); fclose(fid); % Read formatted data, skipping header lines (text) [temps(:,1),temps(:,2)] = textread('Temperature.table','%f%f','headerlines',2); disp(temps) %% A line plot with several plots overlayed t=linspace(0,2*pi,100); % Generate vector t y1=sin(t); y2=t; y3=t-t.^3/6+t.^5/120; plot(t,y1,t,y2,'--',t,y3,'o'); % Plot 3 curves axis([0 5 -1 5]); % Set axes xlabel('t');ylabel('Approximations of sin(t)'); title('First example graph'); legend('sin(t)','Linear approximation','3 terms of Taylor series'); % print -deps plot1.eps % Can also overlay plots using hold and line commands %% A contour plot r=-5:0.2:5; [X,Y]=meshgrid(r,r); Z=-0.5*X.^2 + X.*Y + Y.^2; cs=contour(X,Y,Z); clabel(cs); %% Surface plots Z=cos(X).*cos(Y).*exp(-sqrt(X.^2+Y.^2)/4); figure;surf(X,Y,Z); figure;surfl(X,Y,Z);shading interp; colormap hot; %% Animation clf % clear other figures theta=linspace(0,2*pi,100);x=cos(theta);y=sin(theta); % Draw bead at initial position and assign a 'handle' (i.e. name) hbead=line(x(1),y(1),'marker','o', 'markersize',8,'erase','background'); htrail=line(x(1),y(1),'marker','.', 'color','r','erase','none'); axis([-1.5 1.5 -1.5 1.5]);axis('square');xlabel('x');ylabel('y'); for k=2:length(theta) % Cycle through all positions set(hbead, 'xdata',x(k),'ydata',y(k)); set(htrail,'xdata',x(k),'ydata',y(k)); drawnow % Draw bead at new position end