Export Data from Matlab to Text Files

First eigenfunction of the L-shaped membrane, ...Image via Wikipedia

There are many different ways to export and import data from matlab. You can import and export data from and to matlab binary formats (MAT files), text files, Excel spreadsheet (works only on Windows), XML, several special purpose formats, and a lot of image, audio, and video file formats. Text formats are very useful, because they very portable in that they can be read and written by many different applications.

In this post I will give some examples of exporting to different text formats. I will also mention how to import data using complementary commands.

The simplest way to export data to text format is this:
save -ascii

Matlab exports data by default in the scientific numeric format. If you want to use these data with some other program outside matlab, this can lead to problems. Although nowadays many programs use libraries that permit reading scientific notation (e.g. boost regex library for C++), it is sometimes better to write to a fixed-digits format.

Using save, the -double option says that you want the numbers in 16-digit format.

To write matrix A to a column separated value (CSV) file, there are several alternatives. dlmwrite is one possibility:
>> dlmwrite('attr20.ascii',A,'delimiter',',');

The default delimiter is already the comma, so the last parameter is unnecessary. If you want your data space-separated this command is your friend:
>> dlmwrite('attr20.ascii',A,'delimiter','\t');

If you use the tabulator as delimiter, you can use also use save:

>> save('attr20.ascii','A','-ascii','-double','-tabs');

The last option I give here is csvwrite:
>> csvwrite('attr20.ascii',A); 

These commands work for vectors and two-dimensional matrices.

Also sometimes useful is diary to save your command history to a disk file. You can view and edit the resulting text file using any word processor.

For importing data to matlab you can use the corresponding commands dlmread, load, csvwrite. Some files you might have to filter before reading them into matlab. For example to get rid of comments. Say the files provide comments at the start of the line starting with the percent sign (%). Then filtering can be done with sed:
> sed -i /^%/d *

Enjoy. Please leave a comment below for questions and suggestions.

You might also be interested in my article on exporting figures from matlab. I also wrote an article about creating videos in matlab.
[ Read more... ]

Handy Nix Shell Commands - Alarm Clock Oneliners and More

Some linux commands are fantastic!

I found some nice ones in a slashdot postings about surprising linux commands and others come from slashdot user tpwch.

Alarm Clocks and Notices

Want a simple alarm clock? Try this one:
echo "cat /dev/urandom > /dev/dsp" | at 7am tomorrow

Your food is in the oven?
sleep $((20*60)); xmessage "Dinner is done"

You can also use zenity to get a popup message:
sleep $((20*60)); zenity --info --text "Dinner is done"

Or, if you prefer a voice message, you can use espeak or festival, which I explain in Free Text-to-Speech.
sleep $((20*60)); echo "Take the food out of the oven" | espeak

Notification of events.

For example, notifying me when some specific thing changed on a website:
CHECKLINE="$(curl -s http://somewebsite.org/somepage.html [somewebsite.org] | grep "currently undergoing maintenence")"
while true; do
sleep 120
[ -z "$CHECKLINE" ] && xmessage "somewebsite is open again" && exit
done

Checking for changes on a website:
while true; do
OLD_MD5=${MD5}
CONTENT=$(elinks -dump 1 -dump-charset iso-8859-1 "http://someurl.com/track?id=someid")
MD5=$(echo -n $CONTENT | md5sum -)

[ "${MD5}" != "${OLD_MD5}" ] && {
xmessage "$(printf "New action: :\n\n${CONTENT}")"
}
sleep 120
done

Prank Greetings

Some nice greetings to your colleague?
cat /dev/random | write colleague [ Read more... ]

Watching the US Presidential Elections 2008

Is it the maverick soldier and his Christian beauty queen? Or rather that Barack Hussein Arab Muslim Obama? See and watch for yourself the Americans wonder of democracy and the final decision.

Here you find a schedule (CST is CET - 7). See also the map on cnn.com.

You can watch live streams directly in your browser (if you have the plugins), or in some player such as vlc or totem. In linux with totem you have the advantage to be prompted for installation of missing plugins.

For CNN the address is http://www.cnn.com/video/live/cnnlive_1.asx.

At CSPAN you can also find 3 streams.

BTW, Linus Torvalds is endorsing Obama as you can see in his blog. [ Read more... ]

Ubuntu 8.10 Released (Intrepid Ibex)

Ubuntu 8.10 is released. You might want to read the release notes and the press release.

I waited until today to try out the ibex on my laptop, because my wireless card from intel had some firmwire issues with the new linux kernel (2.6.27) which weren't fixed in 8.10 for the beta version.

A lot of people are downloading from the servers. Before you start, make sure you have configured a repository, which is close to you (go to system->administration->software sources->download from->other-> select best server). You might also want to try out apt-p2p. Read a tutorial here.

If you are ready to upgrade to intrepid, press alt-f2 (or open the terminal) and type
update-manager -d

At first I had sound issues on my office computer, but killing and removing pulseaudio and switching to OSS worked (see forum post). [ Read more... ]

Access machine in private network with shared ip

Suppose your computer at work is in a private network that does IP masquerading. You want to access that machine from home or from another computer outside the network. Suppose you have sent your admins about 30 mails in about 14 weeks about you wanting to access your machine from outside and suppose they answered about a third of the mails, but you haven't made any significant progress towards accessing your machine. I am not talking hypothetically here unfortunately.

Actually it's quite simple if you use reverse ssh tunnels (thank you howtoforge). Say you want to access from a machine, which we call second_machine. From your machine at work you type (as root):
# ssh -R 19999:localhost:22  user@second_machine 


From second_machine you can now access your machine at work:
ssh localhost -p 19999


By default this reverse tunnel should stay alive with the ssh session from your machine at work to the second_machine. You can change the time this session stays alive in second_machine:/etc/ssh/sshd_config with ClientAliveInterval n. Default is 0, which means there is no automatic logout. You may also use autossh to automatically restart the ssh tunel.

If you copy files by scp use the -P switch to specify the port. With rsync, rsync ... -e 'ssh -p 19999' .... [ Read more... ]