Task: Search through a bathymetry array to find locations (lat, lon) that are deeper than 6000 m. Print the depths and locations to a data file. The bathymetry file is NAbathy.mat containing a 2-D array of elevation (NAelev) and 1-D location arrays: NAlat and NAlon. Positive elevation is above sea level, negative are below.
An alternative way to search an elevation data set:
% load Elev, lat, and lon load('NAbathy.mat') % don't use data=load('NAbathy.mat'); This will put the variables in % a structure (more about them in a bit) % with names: data.NAelev, etc. Target=1000; % or some choice (above sea level is positive) [nr nc]=size(Elev); ir=[]; % create empty arrays to hold location indexes ic=[]; for r=1:nr % loop over rows for c=1:nc % loop over columns if (Elev(r,c) > Target) % elevation above the target ir=[ir r]; % save row index ic=[ic c]; % save column index disp(['lat, lon: ' num2str(lat(r)) ', ' num2str(lon(c))]) end end
Two new features appear in this script. First, is the creation of an "empty list" indicated by the concatenation operator ([]) with no contents. Then the indexes of the points of interest are added to the end of the list as they are found. Since we don't know ahead of time how many points there are, we just keep track of them as we find them.
The second feature is nested for loops which means that there is a loop inside of a loop. In this case, the outer loop sets the value of r=1, then the inner loop runs through all values of c from 1 to nc. Then the outer loop sets r=2 and the inner loop again runs through all values of c from 1 to nc. In this way, the loops look at all values in the 2-D array. Recall that r and c indicate locations in the table, indicating which row and which column, respectively, where the specific value is located.
Notice also that the nested loops are indented. That is, the lines in the body of the loop are shifted to the right by two spaces. This makes clear what lines are inside each loop body. This can be particularly important for multiple nested loops.
We have seen how to print to a data file in various ways. A last method, introduced here, is a very explicit way to print to a file which gives you complete control over the result. This method requires that you open access to a file, write values one line at a time, then close the file.
Open a file with the command fid=fopen('file.dat','w'); where the first entry is the name of the file and the second requests permission to write to the file (or to create a file if it does not already exist). The output of this command, fid, is the identifier for the file (it is a number identfying the path to the file but the actual value is not important to you).
After all writing is finished, release access to the file with fclose(fid);.
Write to this file with the command fprintf(fid,format,variables);. An example is fprintf(fid,'%5d %5.2f %5d\n',a,b,c); which writes the values of variables a, b and c using integer format (%d, where %5d says to use 5 spaces for the number) for a and c and real format (%f, where %5.2f says to use 5 spaces with 2 digits after the decimal for b. The \n character in the format which signals a "newline" which terminates writing to the current line and moves to the next line. You can add stuff to an output line with multiple fprintf commands and shift to a new line with \n.
The example above could also be done with individual print statements
fprintf(fid,'%5d ',a); fprintf(fid,'%5.2f ',b); fprintf(fid,'%5d ',c); fprintf(fid,'\n');
Any character in the format string that is not a format or a newline, will be written to the file. So the command fprintf(fid,'The value of a is %5d\n',a); will write the text as given and insert the values of the variable in the place indicated by the format.
There are many formats which you can find with help fprintf or doc fprintf. Strings can be written with the %s format, where %10s says to use 10 spaces for the string.
You can print the high elevation points in the previous script with the following:
n=length(ir); fid = fopen('HighPoints.dat','w'); for i=1:n r=ir(i);c=ic(i); fprintf(fid,'%5d %5d ',r,c) fprintf(fid,'%10.2f %10.2f ',lat(r),lon(c)) fprintf(fid,'%10.3 \n',Elev(r,c)) end fclose(fid);Each line in the file will have the row and column index, the lat and lon (to the hundredths place), and the elevation (to the thousandths place).
In the example above, the information printed on a line is done in several steps. The end of the line is indicated by printing the "end of line" or "newline" character \n.
Flow chart for task:
%%% Load data file %%% set target depth %%% get size of array %%% open output file %%% loop through depth array %%% test for depths greater than target %%% print depth and locations %%% close output file
Script to complete task:
%%% Load data file load('NAbathy.mat') %%% set target depth Target=-6000; %%% get size of array [nr nc]=size(Elev); %%% open output file fid=fopen('DeepLocs.dat','w'); fprintf(fid,'Lat, Lon, Depth \n'); % header line %%% loop through depth array for r=1:nr % loop over rows for c=1:nc % loop over columns %%% test for depths greater than target if (Elev(r,c) <= Target) % depths are negative %%% print depth and locations fprintf(fid,'%8.3f %8.3f ',lat(c),lon(r)); fprintf(fid,'%8.1f\n',elev(r,c)); end end end %%% close output file fclose(fid);