Task: Create a set of 6 functions to convert temperature among fahrenheit, centigrade and Kelvin. Create a folder in your top directory called "matlab/MyTools" (or something similar) and put these functions there. Use addpath to gain access to the folder. Write a script to test your routines.
The real benefit of functions is that it allows you to create (or import) general tools that you can use for your work. It is not convenient to copy each useful function to each work directory. Nor is it useful to always work in a single work directory with all scripts, functions, and data in one place.
MATLAB defines a search path (a list of folders) where it looks for functions and files. When you download MATLAB, it installs a bunch of functions in folders in your Applications directory (or some such similar standard place). It then creates a default search path so these functions will be available to you. You can see the whole search path by typing path at the command prompt or by going to the setpath menu on the MATLAB page.
You can use this structure to set up (or import) your own tools and add the folder(s) to the search path.
The search path can be modified temporarily for a given script or it can be modified permanently so it will always be available.
Suppose I set up a toolbox in the directory "/Users/klinck/matlab/MyTools/". Then any functions that I create can be put there and I get access to them with the command:
addpath('/Users/klinck/matlab/MyTools/')or
addpath /Users/klinck/matlab/MyTools/This is the mac OS X or Unix format. Windows will use something like
addpath c:\Users\klinck\matlab\MyTools
Either of these commands will add this directory to the beginning of the search path. You only need to do this once in a given MATLAB session. Every time you run this command, the file name is added to the beginning of the path list.
There is another option. When matlab starts, it looks for a file called startup.m in the folder where the program starts. If you click an icon, then matlab starts in the location where it is installed. If you start with a line command in a different folder, matlab will look for the startup file in that folder. In any cases, you can use the startup script to define new folders to add to the path. With this procedure, you will only add to the path one time during the session.
It is also possible to make a permanent change to the search path. On the MATLAB window, there is a pulldown menu called "Set Path". From this menu it is possible to add a folder to the search path. Saving this information adds the new folder permanently to the search path.
There are many tools written for MATLAB that are available on the web. A number of ocean related tools are at a web site called SEAMAT, which is supported by USGS at Woods Hole. An outdated toolbox called OCEANS has tools to calculate seawater density, freezing point and many other quantities.
These routines may be in zip or tar files. These last two commands are MSDOS and UNIX commands to collect files into a single package. Most computer operating systems have tools to expand and uncompress either of these file types.
The modern tools for ocean physical properties are in the GSW (Gibbs SeaWater) toolbox (www.teos-10.org).
Any number of tools are available either at mathworks.com or on web pages provided by individuals. A web search for "matlab TOPIC" will often provide a number of options for tools to perform specific numerical calculations identified by this TOPIC.
Temperature conversion formulas for Centigrade, Farenheit or Kelvin scales:
C = (F-32)*5/9; F = C*9/5 + 32; K = C + 273.15; C = K - 273.15; F = (K - 273.15)*9/5 + 32; K = (F - 32)*5/9 + 273.15;
The flow chart to accomplish the task
%%% define test routines (convert a temperature from a scale and back) %%% define function to convert F -> C %%% define function to convert C -> F %%% define function to convert C -> K %%% define function to convert K -> C %%% define function to convert K -> F %%% define function to convert F -> K
All of the functions need to be put in separate files with the names of the functions. Then all files put into a single directory. The script to accomplish the task is
%%% define test routines (convert a temperature from a scale and back) addpath('/Users/klinck/matlab/MyTools/') disp([' 0 C to F: ' num2str(F83cf(0))) ' to get 32F']) disp([' 212 F to C: ' num2str(F83fc(212))) ' to get 100C']) disp([' 0 C to K: ' num2str(F83ck(0))) ' to get 273.15K') disp([' 32 F to K: ' num2str(F83fk(0))) ' to get 273.15K') T=50; disp([' Convert from C to F and back: ' num2str(F83fc(F83cf(T))) ' to get ' num2str(T) ]) disp([' Convert from C to K and back: ' num2str(F83kc(F83ck(T))) ' to get ' num2str(T) ]) disp([' Convert from F to K and back: ' num2str(F83kf(F83fk(T))) ' to get ' num2str(T) ]) %%% define function to convert F -> C function c=F83fc(f) % converts fahrenheit to centigrade % calling sequence: % c=F83fc(f) c = (f-32)*5/9; end %%% define function to convert C -> F function f=F83cf(c) % converts centigrade to fahrenheit % calling sequence: % f=F83cf(c) f = c*(9/5) + 32; end %%% define function to convert C -> K function k=F83ck(c) % converts centigrade to kelvin % calling sequence: % k=F83ck(c) k = c+273.15; end %%% define function to convert K -> C function c=F83kc(k) % converts kelvin to centigrade % calling sequence: % c=F83kc(k) c = k-273.15; end %%% define function to convert K -> F function f=F83kf(k) % converts kelvin to fahrenheit % calling sequence: % f=F83kf(k) f = (k-273.15)*(9/5) + 32; end %%% define function to convert F -> K function k=F83fk(f) % converts fahrenheit to kelvin % calling sequence: % k=F83fk(f) k=(f-32)*(5/9) + 273.15; end