Reading numbers from a string in MATLAB

I always have needs to find numbers from a formatted string using Matlab. For example, find the lats and longs for all the grid cells of a river basin based on a bunch of input files, something like (forcings_35.25_-100.75). Then in Matlab you have two ways to read these numbers out.

  • using ‘sscanf’. It’s kinda stupid that you can’t find the two numbers by
  • sscanf('forcings_35.25_-100.75','%s_%f_%f')

    instead, you will get an 1d array that contains 22 numbers, with each number representing a single character in that string. What you need to do is using

  • sscanf('forcings_35.25_-100.75','forcings_%f_%f')

    I guess the reason behind this is that sscanf can only output one numerical matrix that based on the first character it processes. So if you do

  • sscanf('35.25_-100.75_forcings','%f_%f_%s')

    you will get the first two number you want, followed by 8 numbers representing the string “forcings”.

  • Another way is to use “textscan”. I feel safer using this method.
  • A = textscan('forcings_35.25_-100.75','%s %f %f', 'Delimiter', '_')

    This way, you will end up getting a three cell array representing the three parts separated by “_” in the string. Then simply using cell2mat() to convert the numbers to regular array. To convert “forcings” to string, using

  • char(A{1})

Using gdb to debug (segmentation error)

  1. compile the code and get the executable (e.g. vicNl)
  2. type “gdb”
  3. in gdb environment, type “file vicNl”
  4. “run -v” # -v is the argument input for vicNl
  5. when it stops, type “bt” to back track the error

How to flip a matrix upside down

So recently we had some problem about flipping a matrix upside down, sounds easy, but not that simple if the matrix is stored as an array. For example, you have a 5*4 matrix like this:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

and it’s written like this


The question is how to flip the matrix upside down without transforming the array to the 2D matrix. After flipping, the array should be like this:


So I wrote a perl script to do this:

#!/usr/bin/perl -w
# this script convert an array to an n*m matrix and then 
# flip the matrix upside down then restore it back to an array

use strict;

my @div = (1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5);
print "@div\n";

my $row = 2;
my $col = 10;

my @out = map{0} 0..$#div; # define an empty array
print "@out\n";

for (my $i = 1 ; $i <= $row; $i++) {
    for (my $j = 1; $j <= $col; $j++) {
        $out[($col*($row-$i)+$j)-1] = $div[(($i-1)*$col+$j)-1];

print "@out\n";

It’s an interesting exercise and it’s useful sometimes when you’re dealing with NetCDF files and Arcmap ascii outputs or converting files between binary and ascii.

Bit, byte, and some other things about binary files

So here are some basics about the binary format in data computing and transferring.

1. Bit is the smallest unit, representing 0 or 1.

2. Seems like the smallest data storing unit is 1 byte, which is 8 bit. There are 8 slots for 0 or 1 in a byte. It can store 2^8 = 256 different integer numbers (for unsigned integer, it’s 0 to 255)

3. Sometimes we need more binary slots for higher data precision. Then we have 16bit short, 32bit float, etc. 32 bit float stores data like this:


where s = sign bit, encoded as 0 => positive, 1 => negative
A = 7-bit binary integer, the characteristic
B = 24-bit binary integer, the mantissa.

4. Beware of the writing and reading orders, i.e. Big or Little-Endian when reading and writing data. Different CPU follows different orders to read and write these binary data. If a float file is written by Big-Endian cpu (like IBM or something) but read by Little-Endian cpu (like Intel) you will easily get things screwed up.

5. There’s a very nice website to help you understand the binary file basics: