Department of Engineering

IT Services

Getting data in and out

If you want to analyse or display a large amount of data in Matlab, or to export a large amount of results from Matlab, you will probably want to read the data from a file or send it to a file. We have already seen the commands load and save, which read and write files, respectively, but when used in their basic forms they will read or write files in a peculiar format understood only by Matlab. This is fine and efficient if the files are only going to be used by other Matlab users or programs, but of no use if you want them for other purposes.

The easiest thing to do is to issue a command such as

 
save myfile.dat sint -ascii

This saves the variable sint to a file called myfile.dat in the `ASCII' format, which is understood by editors, for instance, and can be read by other software. The command

 
load myfile.dat

reads the file myfile.dat from your directory.

Consult the reference section of the User's Guide to get details of how incoming ASCII files should be laid out.

For loading moderate amounts of data (small enough to type in by hand, large enough for mistakes to be likely and irritating) you can just create a script file which defines the variables you want. For instance the file data.m may contain the text

	weights = [1.23 2.34 3.45 .12 .234 .345 1.23456 32.1 234 ...
		21 32 43 54 123 ]
	locs = [ -2 3 12 -4 23 1.34 5432 34 56 98 67 234 53 45]

Typing data within Matlab gives the variables weights and locs these values.