MATLAB Tips
  Comments or problems: Contact Dr. Timothy D. Placek
All Pages Copyright 2002 by Auburn University, Auburn, AL 36849

 

The "for" Statement


One of the main elements of programming is looping. Within MATLAB there are a variety of ways to create program loops to achieve "repetition".

The general form of a simple "for loop" is

>>for i=1:n,
        something to be done;
        something else to be done;
end

Notice that there is a "comma" at the end of the line starting the "for loop". This is required !!!

Here is an example of plotting the of sin(ax) against x for a series of different "a" values.

>> x=0:0.01:1.0;
>> c=[1,2,3,4];
>> hold on;
>> for i=1:length(c),
          a=c(i);
          plot(x,sin(a*x));
>> end

The plot obtained is:


While on the subject of handling graphs, be aware that it is trivial to export your figures in many convenient formats. For example, you can use the popular jpg format or bmp, etc.

Just go to the "File" menu in the plot window and select "Export" and choose the format you wish to store the graph in. Nothing hard at all about it !!

 

 

  

 

Comments or problems: Contact Dr. Timothy D. Placek
All Pages Copyright 2002 by Auburn University, Auburn, AL 36849
[AU Logo]