Beruflich Dokumente
Kultur Dokumente
Plotting
figure figure(x) plot plot3 semilogx semilogy loglog polar surf / mesh contour hold on / off axis([x1 x2 y1 y2]) axis equal title(string) xlabel, ylabel, zlabel legend text grid subplot axes close close all Create a new figure Create the figure x, or make figure x active 2-dimensional plotting routine (type help plot for more info) 3-dimensional plotting routine 2D plotting with logarithmic x-axis 2D plotting with logarithmic y-axis 2D plotting with logarithmic x and y-axes Plot in polar coordinates Create a surface / mesh Create a contour plot Allow more plots to be added to the same axes Set the axes Make the axes equal in height/width (type help axis) Set the title of a figure Set the x, y, and z labels Add a legend to a figure Add text to the plot Add a grid to the plot Place multiple plots in the same figure (type help subplot) Creates a new plotting axis in the figure (type help axes) Closes the current figure, i.e., close(gcf) Close all figures
A few other things: >> plot(x,y,'r','linewidth',2) will make a red line of thickness 2 >> plot(x,y,'m--','linewidth',5) will make a magenta dashed line of thickness 5 >> plot(x,y,'g.','markersize',10) will make a set of green dots of size 10 >> plot(x,y,'k*','markersize',10) will make a set of black stars of size 10 Type help plot for more information
Handles
All figures, axes, structures, etc have handles associated with them. They are similar to pointers in other languages. The user may display any property of an object by looking at the right handle; the user may also set any property in an object by setting the right property of the corresponding handle. Here are some examples: figure(10) Create a new figure with the handle 10 gcf Get the current figures handle (10) get(10) Displays lots of properties about that figure! x = rand(10,1); Produce random vectors for x and y y = rand(10,1); h1 = plot(x,y) h1 is the handle of the plotted line get(h1) Display all of the properties of the line set(h1,Color,[.2,.7,1]) Set the color of the line to teal gca Get the current axis information set(gca,YScale,Log) Set the current y-axis to be logarithmic set(gca,XAxisLocation,top) Move the x-axis labels to the top MATLAB allows the user to do pretty much anything that is desired. Its a lot of work, but it can be rewarding.
Input/Output
The user may write to and read from files. The following commands are useful; type help <command> for more information about these. fopen fclose fread fwrite fscanf fprintf save load frewind diary input keyboard Open a file for reading or writing Close the file (anything written doesnt actually get written until you do this!) Read binary data from a file Write binary data to a file Read formatted data from a file Write formatted data to a file Save workspace variables to disk Load workspace variables from disk Rewind the file Save text of MATLAB session Prompt for user input Invoke keyboard from M-file
The find command (among others) requires logical operators, including the following: == or eq() Test equality ~= or ne() Not equal < or lt() Less than
Greater than Less than or equal to Greater than or equal to Logical AND Logical OR Logical NOT Logical XOR (Exclusive OR) True if any element is nonzero True if all elements are nonzero
Random Numbers
rand rand('state',sum(100*clock)) randn Uniformly distributed random number generator Ensure a new seed each time Normally-distributed random number generator
Functions / Scripts
New functions may be added to MATLAB's vocabulary if they are expressed in terms of other existing functions. The commands and functions that comprise the new function must be put in a file whose name defines the name of the new function, with a filename extension of '.m'. At the top of the file must be a line that contains the syntax definition for the new function. For example, the existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector. The variables within the body of the function are all local variables. Type help function for more information about functions and subfunctions. Other function commands: return varargin / varargout nargin / nargout inputname mfilename Force an early return from a function Variable length input/output argument list Number of function input/output arguments Input argument name Name of currently executing M-file
A SCRIPT file is an external file that contains a sequence of MATLAB statements. By typing the filename, subsequent MATLAB input is obtained from the file. SCRIPT files have a filename extension of ".m" and are often called "M-files". Variables contained in the script are external variables; variables declared in the script are usable outside of the script. Function calls tend to take less time during compilation because they are only compiled once, whereas scripts are compiled each and every time they are called. Thus, functions tend to be faster if called numerous times.
One other note: The user can evaluate a string as a function/script call by using the eval command. The sprintf command helps to convert formatted text to a string.
Structures
MATLAB, like many other languages, has the capability to construct user-defined structures. struct . isfield fieldnames getfield setfield rmfield deal Create a structure, or convert an object to a structure Access a structure field Determine if a field is in a structure Get structure field names Get structure field contents Set structure field contents Remove structure field Deal inputs to outputs (assign several elements simultaneously)
Miscellaneous
global tic / toc clock abs sign round, ceil, floor, fix mod / rem i / j ! who whos what which lookfor lookfor -all exist Declare global variables Computes the amount of time required to perform some operations Current date and time Absolute value The (+/-) sign of a variable Round a number in various ways Modulus / remainder Imaginary units, sqrt(-1) System Call List current variables List current variables, long form List functions in a given directory Finds the directory containing a given function or file Finds all functions in all directories in the path that might have something to do with a given key word Searches harder Checks to see if variables or functions are defined