JISAO data

MATLAB string and structure manipulations, and miscellanea

I find the handling of strings and structures in MATLAB to be completely arbitrary and the documentation on it to be too terse. The following are conventions that I have figured out or other people have explained to me.

1) Suppose you do a bunch of calculations and you end up with result variables with names like precipensondjfmacor, precipensondjfmareg, sfctemppnandjfmacor, and sfctemppnajasreg; and you would like to plot these fields and write them out as PostScript files. You need a way to convert the variable names into strings that can be used in figure titles and as output filenames.

  • Use "whos" to generate a list of variables, save that list as a file, and edit the file down to just the variable names.
  • Use fgetl to ingest the variable names as strings, for example, string = 'precipensodjfmacor'
  • eval( [ 'a=' string ';' ] ) will execute the command "a = precipensodjfmacor;" in Matlab.
    A sublety in this is that the "eval" command requires spaces to parse the statement on what to do. For example, eval(['a='string';'] ) will generate the error: "Error: Unexpected MATLAB expression." "help eval" says nothing about this.

    A second example is how to get Matlab to do something outside of the matlab session. Say you wanted to delete a file from within Matlab:
    eval( [ '!/bin/rm ' string ';' ] )
    will delete the file "precipensodjfmacor"

    2) An application of "eval."

    "Eval" can be used to access the contents of a variable. An example of this is variablename = 'summertemperature'.
    eval(variablename) yields the contents of variablename, which is 'summertemperature.' This is useful in a subroutine call where you want to pass numbers and not just a variable name.
    subroutine( varname ) will crash
    subroutine( eval(varname) ) will pass the variable 'summertemperature'

    3) Arguments to the print function.

    Continuing on with using string = 'precipensondjfmacor', let's say that you want to print out this analysis using the print function.
    print( '-dpsc2', sprintf( '%s.ps', string ) );
    will create the PostScript file 'precipensondjfmacor.ps' (no quotes). It appears that the inputs to the print function need to be strings, for example, '-dpsc2' requires the single quotes.

    4) Decoding time

    Time in netcdf files can be written as so many units since some reference time. An example of a reference time is January 1st, 1999, which is written as "1999-01-01 0:0:0". The time variable "time" can be written in days, hours, seconds, or something else.

    The Matlab functions datevec and datenum can be used in the following manner to convert from time in some silly units to a vector of the year, month, day, hour, minute, and second:

    datevec( time + datenum([1999,01,01,0,0,0]) )

    where "time" is the time variable. The datenum output and datevec input are both hours so "time" has to be in units of hours.

    Example: Convert 17288910 seconds since 1999-01-01 0:0:0 into years, months, days, ... :
    datevec( 17288910/60/60/24 + datenum( [ 1999 1 1 0 0 0 ] ) )
    yields: 1999 7 20 2 28 30 which is 2:28 AM and 30 seconds on the 20th of July, 1999.

    Guillaume Mauger showed me this trick.

    5) Creating a structured array.  An example:

    c = struct( 'pre1', pre1, 'pre2', pre2 ) yields

    c =
    pre1: [400x1440 double]
    pre2: [400x1440 double]
    6) Modifying/changing the value in a structured array

    Suppose you read in the header of a netCDF file with ncinfo, info = ncinfo( filename.nc ); . "info" will be a structured array with a mix of character and numerical data. The number of latitudes, longitudes, and time values is stored in info.Dimensions.Length . To change the number of latitudes, use info.Dimensions(1).Length = new_value, where the (1) specifies that it is the latitude dimension you are changing.

    7) Converting numbers in cells to numbers: number = cell2mat(cell_variable)

    8) Try something (process A), and do something else (process B) if it fails.

    process A
    process B


    October 2015
    Todd Mitchell ( mitchell@atmos.washington.edu )
    /home/disk/user_www/mitchell or ~mitchell/public_html/
    JISAO data