Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

What Data Is Available?

Yes, I know, data are, but the title just sounded terrible that way.

Ever wonder what data you have stored in some files, particularly MATLAB MAT-files? Sometimes I'd like to know what's there without making the commitment to load the data. Fortunately, there's a good way to accomplish this in MATLAB programmatically, and another good way when viewing the Current Folder. I will show you the programmatic way.

Contents

Files to Look at

Let's first identify some files to look at. I happen to know that we have some data for demos stored on the MATLAB path. These include the files durer.mat, seamount.mat, and clown.mat.

Peek Inside

Let's take a peek inside the seamount data. I can do this using a variant of the whos function.

whos -file seamount.mat
  Name           Size             Bytes  Class     Attributes

  caption        1x229              458  char                
  x            294x1               2352  double              
  y            294x1               2352  double              
  z            294x1               2352  double              

And what's lurking inside topo.mat?

whos -file topo
  Name              Size              Bytes  Class     Attributes

  topo            180x360            518400  double              
  topolegend        1x3                  24  double              
  topomap1         64x3                1536  double              
  topomap2        128x3                3072  double              

Notice in this case, there are 4 variables, including 2 starting with the name "topomap". Suppose I want to find all the variables with names beginning with "topom". I can use the wildcard character "*".

whos topom* -file topo
  Name            Size            Bytes  Class     Attributes

  topomap1       64x3              1536  double              
  topomap2      128x3              3072  double              

Loading Some Data

Assuming I have enough memory, I can easily load the data from a MAT-file. But sometimes I don't want to load everything, perhaps because some of the variables are large. And if I don't need them, why use the memory and clutter my workspace? I can choose to load selected variables only.

NOTE: Before anyone asks, I can't currently (R2010a) load part of an array in MATLAB; and yes, that's on our enhancement list.

So let me load in just the topomap* variables. I have two ways to manage this. First

load topo topom*
whos
  Name            Size            Bytes  Class     Attributes

  topomap1       64x3              1536  double              
  topomap2      128x3              3072  double              

and you can see that the variables topomap1 and topomap2 have been loaded into my workspace. There have been many discussions on the MATLAB newsgroup that "poofing" variables into the workspace, especially inside a function, is a poor idea. Instead, I can load them into a known variable. To do so, I must use the functional form of load instead of the command form.

mydata = load('topo','topom*')
mydata = 
    topomap1: [64x3 double]
    topomap2: [128x3 double]

Now I have a known variable (mydata) which is a struct. mydata has fields with the names of the selected variables from the topo.mat MAT-file.

Here's more information to help you as you manage loading your data into MATLAB.

Do You Load Data Selectively?

Do any of you use some of these techniques for managing and loading selected portions of your data? I'd love to hear more here.




Published with MATLAB® 7.10


  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.