Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

April 23rd, 2008

MATLAB Publishing for Teaching

Since MATLAB has had the capability to publish M-files, we have seen publishing being adopted at many educational institutions. And there is a variety of ways for you to find out just what's been published out there, perhaps so you can leverage it.

Contents

How to Find Published M-files

First, how can you find published M-files on the web? Here are some techniques.

Some Good Educational Sites

You’ll find pointers to a lot of educational sites at the web demos index. I know that some good files are to be found at

But you can just poke around and find a lot of good stuff up and down this list. Here's a really good site I noticed recently for teaching Calculus.

Who Publishes?

I’ve been very happy, as I look at the web demos coming in, to see that published M-files are being used by professors more and more. Some of them are lovely and well made. Here are a few favorites of mine:

What are People Using Published M-files for?

People seem to be using published M-files for educational purposes in three ways, sometimes melding together the first two.

  • demos, concepts, explanations, class notes
  • homework assignments
  • adminstering exams

Homework Examples

Sometimes they’re used for homework assignments. Here are some examples from NJIT.

Example of an Exam

And sometimes they’re even used for exams such as this site.

Do You Publish from MATLAB for School?

If you use publishing for some aspect of school work, post here and please post 1 (one) URL to your own best site. Please also post a URL to a site (new to you perhaps) that uses |publish and you think has material potentially useful to you and others.


Get the MATLAB code

Published with MATLAB® 7.6

April 19th, 2008

Blog Comments

I intend to have as few rules as possible about comments on this blog. I may add new rules as the need arises.

  1. I will delete or not answer comments that consist largely of questions about homework. (Also, I do not answer homework questions sent to me by e-mail.
  2. I will delete or not answer requests for "codes."
  3. I will delete comments that are off-topic for the posted blog.
  4. I will delete product advertisements.

April 17th, 2008

Processing a Set of Files - Repost

This is a repost of an article I wrote early on in this blog's history. Since there continue to be frequent questions on the MATLAB newsgroup regarding processing a set of files, I thought it would be worthwhile recapping this post.

Contents

Setup

I should start a clean workspace and with no WAV-files in my blog publishing directory.

clear all
delete *.wav

Problem Statement

Suppose I want to convert sounds stored in MATLAB MAT-files to files saved in WAV format for Windows. Without explictly hardcoding in the filenames, here's a way to proceed.

Collect the MAT-files Containing Sounds

matfiles = dir(fullfile(matlabroot,'toolbox','matlab','audiovideo','*.mat'))
matfiles = 
6x1 struct array with fields:
    name
    date
    bytes
    isdir
    datenum

Check out the Files

We can see from the length of matfiles that I have 6 MAT-files.

load(matfiles(1).name)
whos
  Name              Size             Bytes  Class     Attributes

  Fs                1x1                  8  double              
  matfiles          6x1               2576  struct              
  y             13129x1             105032  double              

Loading the data places the MAT-file contents into a structure from which I can extract the information I need and write it back out. The sound files that MATLAB ships with store the data in y and the sampling frequency in Fs. Let's look at the first signal.

N = length(y);
plot((1:N)/(N*Fs),y), title(matfiles(1).name(1:end-4))

Loops over the Files

for ind = 2:length(matfiles)
    data = load(matfiles(ind).name);
    wavwrite(data.y,data.Fs,matfiles(ind).name(1:end-4));
end
Warning: Data clipped during write to file:gong
Warning: Data clipped during write to file:splat
Warning: Data clipped during write to file:train

What about Those Files?

dir *.wav
gong.wav      laughter.wav  train.wav     
handel.wav    splat.wav     

Read a File Back for Verification

I'll double-check the last file I just read in and wrote out. ind is still set despite no longer being in the for loop. Let's check both the frequencies and the signals themselves.

[ywav, Fswav] = wavread(matfiles(ind).name(1:end-4));
eqFreqs = isequal(Fswav, data.Fs)
datadiff = norm(ywav-data.y)
eqFreqs =
     1
datadiff =
    0.0010

The data stored in the WAV-file is NOT exactly the same as that stored in the MAT-file. Reading the help for wavwrite gives some insight; the data in the WAV-file, by default, is stored as 16-bit data vs. MATLAB's standard 64-bit double.

Thoughts?

Does it confuse people here that I don't worry about vectorization? Any other thoughts on this topic?


Get the MATLAB code

Published with MATLAB® 7.6

April 8th, 2008

Clearing the Air

We have heard repeatedly over the years that users would like a tool to help clean up the workspace by choosing selected variables to retain. The function clear, has logic allowing you to clear a selection of variables readily, but it's a bit harder to retain a collection using clear.

Contents

What Does clear Clear?

The function clear, with the all qualifier, can clear just about everything in MATLAB: variables, functions, MEX-files, breakpoints, Java packages import list (from the command prompt). To additionally clear classes, use clear classes.

Debug Gotcha

clear all clears all breakpoints as well as the workspace, and everything else that isn't nailed down (or perhaps mlocked ). This is a frequent source of confusion for people when they are trying to debug code. If the code being debugged issues a clear statement, the breakpoints get cleared and you could be left with a temporary mystery until you chase down the code doing the clearing.

How to Keep Variable Selection

  • Use clear with -regexp to remove variables selected from memory.
  • Use clearvars -except to retain specific variables in the workspace.

Here's an example of clearing all the variables except those that start with q (the 17th letter of the English alphabet).

a1 = 'Alice';
a2 = 'Ashley';
a3 = 'Alistair';
b1 = 'Barbara';
b2 = 'Brian';
i1 = 'Isobel';
l1 = 'Loren';
q1 = 'Quentin';
q2 = 'Quillan';
r1 = 'Rosie';
r2 = 'Rob';
who
Your variables are:

Fs        a2        data      ind       q2        ywav      
Fswav     a3        datadiff  l1        r1        
N         b1        eqFreqs   matfiles  r2        
a1        b2        i1        q1        y         

clearvars -except q*
who
Your variables are:

q1  q2  

Your Clear Ideas?

Got any clear ideas? I'd love to hear them; you can post them here.


Get the MATLAB code

Published with MATLAB® 7.6

April 1st, 2008

MATLAB Language Translator

Google recently made a splash about its e-api-tools-for.html latest language tools. Translating one computer language to another is useful but passé. But suppose you could translate a computer language to a human language: MATLAB -> English and English -> MATLAB. Type in your word problem and get the code instantly. It could help you with your MATLAB accent, or, if English is not your first natural language, you might learn some vernacular from MATLAB's interpretation.

Contents

Some Translations

In particular, such a translator might be good at recognizing and translating common phrases. Here are but a few of the phrases the translator (under development) gets right.

  • eye(newt), from Shakespeare's hags
  • ceil(approval), from Good Housekeeping
  • wait(theWorld)
  • angle(repose), an excellent book by Wallace Stegner
  • balance(power), axis(power), etc., assuming you've pre-empted the function with your own variable or function, of course
  • gallery(art)
  • length(day) (day has the same caveat as power above)
  • residue(ashes)
  • sign(theTimes)
  • sound(music), thanks to Julie Andrews, among others
  • theTotal = sum(theParts)

Notice how the parentheses are pronounced in English : "of".

Command Duality

The MATLAB translator understands command-function duality and the nuances of English. It would not do to write the last example as

  • wait theWorld

Nor would

  • polar bear

be okay because polar doesn't take a character array for its first argument.

However, it would be okay to say

  • global warming
  • median strip

What Are Your Translation Needs?

Sure, we will translate between MATLAB and other languages once we have the English version ready to go. And, I should warn you that Steve Eddins from the Steve on Image Processing blog has been working for years on mindread.m as well. Beyond these two obvious needs, what other translation needs do you have? Or other thoughts on this post? What about MATLAB poetry (Tim?)? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.6

March 27th, 2008

A Way to Automate “Regular” Renaming

Recently someone at MathWorks asked me how he could automate the renaming of a bunch of M-files containing underscores ('_') in the names with derived names that removed the underscores and used camelCasing instead. You may have similar name manipulation operations you need to perform.

Contents

My First Attempt

Of course I resorted to using MATLAB for the task, despite other options. I chose the following requirements.

  • Don't worry about leading _
  • Don't worry about cell arrays of strings or string matrices (vectors only need apply)
  • Do worry about multiple consecutive _
  • Do worry about trailing _

Some Sample Names

I first create a list of some sample names so I have a test suite to try out.

names = {'foo_bar','foo_bar_','foo__bar', ...
    'foo_bar__', 'foo_3','foo_3_','foo_3a', ...
    'foo_bar____baz___234___'};
allnames = names'
allnames = 
    'foo_bar'
    'foo_bar_'
    'foo__bar'
    'foo_bar__'
    'foo_3'
    'foo_3_'
    'foo_3a'
    'foo_bar____baz___234___'

My Solution

Let's first try out my solution on these.

for name = names
    disp(camelCase(name{1}));
end
fooBar
fooBar
fooBar
fooBar
foo3
foo3
foo3a
fooBarBaz234

And now let's look at the code.

type camelCase
function y = camelCase(x)
%camelCase Convert name with underscores to camelCase.

% find the underscores 
indall = find(x=='_');
% figure out where consecutive _ are 
% and remove all but the last 
consec = diff(indall)==1;
ind = indall;
ind(consec) = [];

y = x;
y(min(ind+1,end)) = upper(y(min(ind+1,end)));
y(indall) = '';

I first find all the underscores. Then I look for consecutive ones since I really only want the last one in each sequence, since it's the following character that I want to turn into upper case. That is, if a following character exists! So I have to check for that too. I then have an array of indices to upper case (though I allow myself to uppercase _ at the end if it's the last character so I don't have to lengthen my input array; upper('_') is the same as '_'). Now, I go back and use the original indices pointing to all the instances of '_' and remove them. Voila!

History Lesson

And then I got some pangs, because I am well aware that MATLAB supports regular expressions. First some history. Did you know that Stephen Kleene, an American mathematician, was the inventor of regular expressions? He has also been credited with developing a very approachable proof to Gödel's incompleteness theorems. And some punster then said, "Kleeneliness is next to Gödeliness".

Using regexprep

My friend, colleague, and regexp guru, Jason Breslau gave me the regexprep solution to the problem. Using the same names as before, I next show you Jason's magical 1-line expression, producing the same output as my M-file above.

for name = names
    disp(regexprep(name{1}, '_+(\w?)', '${upper($1)}'));
end
fooBar
fooBar
fooBar
fooBar
foo3
foo3
foo3a
fooBarBaz234

Conclusions

My code is still easier for me to understand, and I conclude from that that I should spend some time trying to master regular expressions. In addition, the regular expression code requires no temporary variables, some of which could be large if the input string is long enough. It also occurs to me that regular expressions are a topic worthy of students learning well in college. What do you think? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.6

March 10th, 2008

Keeping Things Tidy

In the past, when you opened a file in a MATLAB program, you had to keep track of it so you could close it under all possible conditions, i.e., if reading or processing failed, worked partially, or worked perfectly. You could achieve this using the catch block after a try. But you still might have to know before closing the file if everything you wanted to do was complete. Code could easily get awkward and buggy if there were lots of code paths to watch out for. With R2008a, you can use the onCleanup class to help you out more cleanly.

Contents

Example Code - Open File

Here's the start of a file I've written that opens up an image file for reading, using fopen.

type openImageFile0
function openImageFile0
% openImageFile Open an image file.
fd = fopen('pout.tif');

If I run it, you can see that I never close the file.

openImageFile0()
fids = fopen('all')
filename = fopen(fids(1));
filename(end-30:end)
fids =
     3
ans =
toolbox\images\imdemos\pout.tif

Tidy up now.

fclose(fids(1));

Example Code - Manage Open, Close, and Errors Using try/catch

Now let's look at another version of the code that will close the file it opens before returning.

type openImageFile1
function openImageFile1
% openImageFile1 Open/process file while ensuring file is 
% closed on finish.
try
    fd = fopen('pout.tif');
    if fid == -1
        error('LS:NoImageFile','no such file')
    end
    doMoreHere(fd);
catch Ex
    disp('Can not read file')
end
if fid ~= -1
    fclose(fid);
end

   

You can see that, even with try/catch, since I always want to close the image file, I have some extra logic at the end of the file. And I have to be careful to know whether the error occurs because the file was never opened, or if instead the error occurs in later processing.

Example Code - Use onCleanup to Ensure File Closes

Here's a file roughly equivalent to the first one, but it ensures that the image file is closed when the function is finished running.

type openImageFile2
function openImageFile2
% openImageFile2 Open an image file for further processing.
%   Note: this doesn't do what you want, since the file will
%   be closed after completing and the file identifier won't
%   be active for use upon completion.
fd = fopen('pout.tif');
C = onCleanup(@()fclose(fd));

To see that this works we first check that no files are open, run the function, and make sure that no files are open afterwards.

fb = fopen('all');
openImageFile2()
fp = fopen('all');
noFilesOpen = isequal(fp,fb,[])
noFilesOpen =
     1

Example Code - Use onCleanup and Process After Opening the File

Let's see the next version of the file.

type openImageFile3
function fd = openImageFile3
% openImageFile3 Open an image file for further processing.
%   Note: this doesn't do what you want, since the file will 
%   be closed after completing and the file identifier won't
%   be active for use upon completion.
fd = fopen('pout.tif');
C = onCleanup(@()fclose(fd));

This function returns the file identifier, but as we'll see, the file is closed once the function returns. This means that any processing/reading we were planning to do with the file after calling the open function will not work (since the file will no longer be open).

fb = fp;
fid = openImageFile3()
fp = fopen('all');
noFilesOpen = isequal(fp,fb,[])
fid =
     3
noFilesOpen =
     1

Even though fid is returned from the function call, the file is no longer open. This is because the onCleanup object C is cleared from existence once openImageFile3 completes execution.

Example Code - Use onCleanup and Process and Close File on Completion

Here's the last version of the file for this blog. As long as you call it with 2 outputs, you are good to go.

type openImageFile4
function [fd, C] = openImageFile4
% openImageFile4 Open an image file for further processing.
%   Note: this does what you want, provided you call the 
%   function with both output arguments.  When you are done
%   cleaned up and the onCleanup object will trigger the file
%   closing.

% Don't even open file if function isn't called with 2 outputs.
if nargout < 2
    error('LS:MustHave2Outputs','must call with 2 outputs')
end

fd = fopen('pout.tif');
C = onCleanup(@()fclose(fd));

This version returns the file identifier and the onCleanup object. Once you have processed your file, simply clear the onCleanup object and the file will close.

To see how this might work, we call it from our function that does the processing.

type run4
function y = run4
% run4 processes an image file.
[fid,C] = openImageFile4();
blocksize = 100;
y = [];
while something
    dat = fread(fid,count);
    y = doSomething(y,dat);
    something = update(something);
end

You can see that run4 calls openImageFile4, and then does the required processing. Because C will vanish once run4 completes, the file will be closed.

Other Uses?

The onCleanup object allows you to more cleanly manage resources such as closing files, closing windows (e.g., last week someone mentioned managing instances of waitbar), managing switching directories. What uses do you foresee for the onCleanup object in your work? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.6

March 4th, 2008

Release R2008a Available

The first 2008 semi-annual release of MathWorks products is now available, R2008a. Over the next few months, some of my blog articles will highlight new features.

Contents

Highlights of Highlights of New Features for MATLAB

Here's a very short list, in no particular order, of some of my favorite new MATLAB features in this release.

What Are Yours?

After perusing the release notes or using R2008a, I wonder what your favorite new features are. Let me know here.


Get the MATLAB code

Published with MATLAB® 7.6

February 27th, 2008

Should min and max Marry?

Do you ever need both the minimum and maximum values on the same data? Sometimes I do, for example, when I want to tweak the limits of a 2-dimensional plot. So I was wondering whether that is a common task? I also wondered what the overhead is in calling min and max separately, instead of scanning through the data once, looking for both the lowest and highest values.

Contents

The Plan

I will explore this idea by creating my own simplified versions of max and min, working only for real vector inputs, not checking for NaNs, and not doing any error checking. Clearly, the code I show is not meant for production but to get to the heart of the algorithm.

Create Data

Here I create a very small test dataset.

n = 10;
x = rand(n,1)
x =
    0.9145
    0.1215
    0.3765
    0.7912
    0.1848
    0.0508
    0.5562
    0.4633
    0.8539
    0.0384

Simplified min function

This function is simple, as advertized.

type mymin
function xmin = mymin(x)
%MYMIN Minimum value of real vector input.
%   MYMIN is a simplified version of MIN that
%   finds the minimum value for
%   real-valued vector.  NaNs are not treated.
xmin = Inf;
for ind = 1:length(x)
    if x(ind) < xmin
        xmin = x(ind);
    end
end

Test mymin Function

Now I compare the result from mymin to the built-in min function in MATLAB.

xmin = mymin(x);
xminML = min(x);
tf = isequal(xmin, xminML)
tf =
     1

Test mymax Function

Do similar testing with mymax.

xmax = mymax(x);
xmaxML = max(x);
tf = isequal(xmax, xmaxML)
tf =
     1

Larger Data

Now I want to do some timing so I will create a much larger array of data.

n = 3e7;
xL = rand(n,1);
tic
xLmin = mymin(xL);
timeMin = toc
tic
xLmax = mymax(xL);
timeMax = toc
timeMin =
    0.7469
timeMax =
    0.7491

Combined Function

Now let me show you my combined function myminmax that loops through the data the same way mymin and mymax did, but does both calculations in the loop together.

type myminmax
function [xmin, xmax] = myminmax(x)
%MYMINMAX Extreme values of real vector input.
%   MYMINMAX is a simplified version of MIN and 
%   MAX combined and finds the
%   minimum  and maximum values for real-valued vector.
%   NaNs are not treated.
xmin = Inf;
xmax = -Inf;
for ind = 1:length(x)
    if x(ind) < xmin
        xmin = x(ind);
    end
    if x(ind) > xmax
        xmax = x(ind);
    end
end

First, let's check that we get the expected results.

[xminNew, xmaxNew] = myminmax(x);
tf = isequal([xminNew xmaxNew], [xmin xmax])
tf =
     1

Time Combined Function

And now let's time the combined function.

tic
[xLminNew xLmaxNew] = myminmax(xL);
timeMinmax = toc
timeMinmax =
    1.0614

Compare Total Times

To compare the times, let's look at the sum for the times calling the individual functions vs. calling the combined one.

t2 = timeMin+timeMax;
format long
disp('Adding separate times     Combined Time')
disp([t2 timeMinmax])
Adding separate times     Combined Time
   1.495988735998570   1.061442445897453

Reset format to default

format short

Worth It?

Is it worth having a combined function for min and max from a speed point of view? I don't know. It depends on many things, including how often it's needed, and if finding the min and max values is one of the dominant time consumers in the overall algorithm. What do you think? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.6

New Simulink Blog on mathworks.com

New Simulink Blog on mathworks.com

I'm very pleased to announce that we have a new blog on mathworks.com. We now have Seth on Simulink. Please read his blog for insight into Simulink, how to use it, what are the new features, etc.


Get the MATLAB code

Published with MATLAB® 7.6


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Loren: Here’s Cleve’s reply to Etienne: The crucial factor is the number and location of the nonzero...
  • Loren: Tristan- Nested functions can be slower in some cases currently. We know we have some opportunities to...
  • Tristan: Wow! I just tried with a global variable and it’s 5 times slower than with a argument! function...
  • Jon: Loren, I encountered this same problem and I attempted to find the answer by looking at the documentation for...
  • Tristan: “One thing that I have long wondered about is relative speed of nested functions relative to...
  • Etienne Non: Hi! I’m trying to understand why the Matlab function LU.m takes almost 20 times more time to...
  • Loren: Jonathan- The behavior you see is because the variable x has to come into inplaceTest and then a copy is made...
  • Jonathan: I am calling it from another function, but have just noticed a bit more odd behavior. Here is what...
  • Loren: Jonathan- To the best of my knowledge (and I just tried it with your example), you can have as many inputs and...
  • Jonathan: Loren, Thanks for pointing me to this blog entry; it is a better approach than manipulating caller...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics