Friday, April 15, 2011

parsing text with Text to Columns

This is one of those things that you just want to hit yourself on the head for not knowing earlier.

In Excel (yes, I still use excel) there is a Text to Columns Wizard that allows you to parse data by either fixed width or a defined delimiter.

In Excel 2011 Mac (gag), it is under Data --> Text to Columns.

PARSING BY FIXED WIDTH IN EXCEL!

This is especially useful for delimiting degrees, minutes, seconds. Use it with Search and Replace to do what you need to do.

It is really sad that I just learned this today.

Friday, March 18, 2011

setting up figure templates

Make your life easier by setting up figure templates for MATLAB

http://gears-tt.blogspot.com/2011/03/templates-why-matlab-is-awesomeness.html

As posted by MATLAB stream on fb.

Friday, December 10, 2010

matlab demo videos

I watched 14 of the demo videos yesterday:

http://www.mathworks.com/products/matlab/demos.html

It took me about 5 years to haphazardly learn most of the things that you could learn in an hour or so watching all of those videos.

Watch the videos.

You learn things like, did you know you can specify something like
mymatrix(10:end);

I've always done
mymatrix(10:length(mymatrix));

Also good:

http://www.mathworks.com/videos/matlab/optimizing-performance.html

I love matlab.

Tuesday, November 30, 2010

plotyy

plotyy - plot with two y-axes, independently control each axis

notes:
(1) specify the x-axis to be exactly the same for both
(2) define the handles for the axes and lines/objects AX(1) is left axis handle, AX(2) is right axis handle

long convoluted example:
    [AX,H1,H2] = plotyy(jda(event),sei(event), ...
        jda(event),dis(event));
    


    set(get(AX(1), 'Ylabel'), 'String', 'seismicity (m/s)', ...
        'FontName', 'Arial', 'FontSize', 12, 'FontWeight', 'bold')
    set(get(AX(2), 'Ylabel'), 'String', 'discharge (m^3/s)', ...
        'FontName', 'Arial', 'FontSize', 12, 'FontWeight', 'bold')
    set(AX(1), 'YLim', [0 .000001], ...
        'XLim', [axismat(1) axismat(4)], ...
        'XTick', axismat, ...
        'YTick', ...
        [0 .0000002 .0000004 .0000006 .0000008 .000001], ...
        'LineWidth',1.5, ...
        'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12) 
    set(AX(2), 'YLim', [0 5000], ...
        'XLim', [axismat(1) axismat(4)], ...
        'XTick', axismat, ...
        'YTick', [0 1000 2000 3000 4000 5000], 'LineWidth',1.5, ...
        'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12) 
    set(H1, 'Marker', 'o', 'LineStyle', 'none', 'MarkerSize', 3);
    set(H2, 'LineWidth', 2);
    axis ([AX(1) AX(2)], 'square');
    xlabel('julian day','FontName', 'Arial', ...
        'FontWeight', 'bold', 'FontSize', 12);

    set(gca, ...
    'LineWidth',1.5, ...
    'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12) 



Monday, November 29, 2010

How to put Excel data into Matlab and retain the 'cell' structure

1. Make your cell structure by using curly brackets:
A={1}
2. Copy the excel data to the clipboard
3. Then open up "A" and right click on the top left cell-- choose 'paste excel data'
4. Save your variable A
5. Use it in Matlab:
B = cell2mat(A(row # to start on : row # to end on , column # );

Monday, October 25, 2010

calculate river slope from coarse DEM

Here is one way to calculate the slope of a stream (and long profile) from a coarse DEM.

(1) If the DEM is huge, clip it to only the area of interest, to speed up the processing. You can do this by creating a polygon outlining the area, adding it to the map, and then using extract by mask (Spatial Analyst, Extraction, Extract by Mask).

(2) It may help to visualize things by making a Hillshade.

(3) Under Spatial Analyst, Hydrology, there are three steps.

(a) Fill

(b) Flow direction

(c) Flow accumulation

(4) Now you want to extract the stream by choosing an upstream point, then selecting all points with a flow accumulation greater than that value, using the conditional to turn the map to 0s and 1s to delineate the stream, and then turn that stream into a vector.

Map algebra, Single Output Map
Conditional

Hydrology
Stream to feature (may be optional and affect choice in Step 5)

(5) Select equally spaced points along this line either by using Hawth’s tools / Geospatial Modeling Environment or this previous post.

(6) Extract Values to Points, Export data

(7) You can open the .dbf in Excel or Gnumeric to inspect the slope.

There’s also this:
Tarboton, D. G., R. L. Bras, and I. Rodriguez-Iturbe. 1991. On the Extraction of Channel Networks from Digital Elevation Data. Hydrological Processes. 5: 81-100.

Monday, October 18, 2010

macro in perl

This is one way to write a macro in perl.

Useful if you want to use perl to manipulate files in the directory, and then run sac commands on them. The sac is run all at once after the if/elsif statements are completed.

macro: A sequence of commands stored for later use in an application

# count number of files in directory

$dir = '.';
@files = <$dir/*>;
$count = @files;

print "numfiles: $count \n";

#######################

# to run sac commands, write to sac macro and run
open(scratch1,'>sacmacro');

# set initial counter
$x = 1;

foreach $fn (<*BHZ*.SAC>){

if ($x==1){

$fnout=$fn . '.merge';
print scratch1 "r $fn \n";
print "read first file: $fn \n";
$x++;

} elsif ($x < $count) {

print scratch1 "merge $fn \n";
print "merge $fn \n";
$x++;

} elsif ($x == $count) {

print scratch1 "merge $fn \n";
print scratch1 "w $fnout \n";
print "merge $fn \n";
}

} # ends the foreach

close(scratch1);

# now run the sac macro
# within the ` `, until EOF, all lines are executed in sac

`sac << EOF
m sacmacro
q
EOF`;