Programming‎ > ‎

MatLab

MATLAB Tips

Here are MATLAB tips. I just started using MATLAB. Matlab uses weird syntaxes which need getting used to.

Index


How can I use a M-file?

Here is the situation. I wrote a M file which has my own function. I found that when I type the function name, Matlab says that it cannot find it. There can be two reasons why Matlab cannot find it. Make sure that the name of the primary funciton must be the same as the name of the file. This means that only one function in a .m file is callable at the command prompt. The rest of the functions in .m file are callable only from the primary function whose name coincides with the filename. You will be writing many .m files :-O.

Next you may have to set your search path. MATLAB interpreter search is done inthe following way when you type foo at the command prompt:

   
  1. Looks foo as a variable.   
  2. Looks for foo as one of built-in functions.   
  3. Looks in the current directory for a file named foo.m.   
  4. Searches the directories on the search path for foo.m.
  

In order to add a particular directory, you do

   
  >> addpath dirname
  

In order to remove a particular path, you do

   
  >> rmpath dirname
  

If you want to know the current search path, then do

   
  >> path
  

You can set the default search path by creating a file called startup.m which should reside in the following directory:

   
  ~/matlab
  

Top


How can I create a grid plot?

I need to create a 2d grid figure with the grey scale as the value of that particular cell. Here is how (note that % is a comment line).

  
  function showdata(item, text)  
  disp(text)  
  % range set to 70 to 85  
  imagesc(item, [70,85])  
  title(text)  
  % colormap('grey')      if you need to be grey scale colorbar  
  % tick mark from 0 to 0.9 with 0.1 interval  
  set(gca,'xticklabel',[0:0.1:0.9])  
  set(gca,'yticklabel',[0:0.1:0.9])  
  % somehow without the following line, tick marks placed wrong.  
  set(gca, 'xtick',[1 2 3 4 5 6 7 8 9 10])  
  xlabel('x')  
  ylabel('y')
  

Top


How can I change a variable name to a string?

When you create a figure, you want to add a title. You want the title to be the variable name. Here is how.

  
  load mydata.txt  
  % now a variable called mydata exists  
  showdata(mydata, 'mydata')  
  % by quoting, you get the string
  

Top

Home

Updated 5/13/2003