Loren on the Art of MATLAB

October 5th, 2006

Code Generation from Import Wizard

Some colleagues and I were talking to some customers recently at a seminar and asked them what some of their favorite new features are. One of the top ones is being able to generate code from the Import Wizard.

Contents

What is the Import Wizard?

Some of you may ask what the Import Wizard is. It's a graphical tool that allows you to load in data to MATLAB without knowing the explicit format of the data. For some projects, you may have many data files with consistent formats, but you don't necessarily know the format until you look at a representative file. With the Import Wizard, you can read in the first data set the way you'd like and generate code for the process. Now you can use the generated M-file to automate importing additional files for future processing.

Working with External Data

In a recent post, I converted a collection of .mat-files containing audio data to a set of .wav files. Since MATLAB already contains wavread for reading in wav-files, we can automate reading in this sort of data already. But suppose instead we had a set of spreadsheets, each containing two columns of data, with headers time and temperature. We could certainly use xlsread, but it would be a bit of work and programming to get the data into two variables named time and temperature.

Using the Import Wizard

Instead, I can use the import wizard to help figure out how I want to represent the data. Let's see what the wizard looks like for the first file: timetempPHX.xls.

You can see that I can read the data in, with column names and other text data placed into cells. I can also read the data into variables, with the column names becoming the variable names in MATLAB.

Now look in the lower right corner and you will see a check box that I now select to generate code for this dataset.

After the code is generated, I save the file as importfileTimeTemp.m.

Using the Generated Code

Now I can read in these files in a more automated way, allowing me to script some analysis. We'll load two such datasets and plot them on the same figure to compare fabricated monthly average high temperatures for a year in Boston and Phoenix.

importfileTimeTemp timetempPHX.xls

Convert months into the correct format for the year 2005.

date = datenum(2005,month,1);
ticks = cumsum([min(date) 31 28 31 30 31 30 31 31 30 31 30]);

Plot the first data set, set x-axis ticks correctly, then get and plot the second set of data. Finally, add a legend and title.

plot(date,temperature,'r*')
set(gca,'Xtick',ticks)
datetick('x',3,'keepticks')
importfileTimeTemp timetempBOS.xls
hold on
plot(date,temperature,'bo')
title('Guesses for average daily high temperatures')
legend({'Phoenix' 'Boston'},'location','northwest')
hold off

What Features Work Well for You?

In the past couple of blog articles, I have tried to show you some of the features new to R2006b. Which features solve a problem for you? Let me know.


Published with MATLAB® 7.3

4 Responses to “Code Generation from Import Wizard”

  1. Maik replied on :

    Hello,

    thank you for the idea to define the ticks by hand.
    like: ticks = cumsum([min(date) 31 28 31 30 31 30 31 31 30 31 30]);

    But when I changed my time horizon it did not work out.
    Then I looked into the datevec() function.
    Now it was easy to ask for the beginning of the month and take this as the datenum() for the ticks.
    Then using datetick and your scale on monthly basis is right!

    Here is my code example:

    tm0=0;tmE=0;
    t1 = datenum(num2str(200304010000),’yyyymmddHHMM’);
    t2 = datenum(num2str(200311010000),’yyyymmddHHMM’);
    for n=1:length(Qm_ho(:,1)),
    if (Qm_ho(n,1)== t1)
    tm0 = n;
    end
    if Qm_ho(n,1)== t2
    tmE = n;
    end
    end
    tm0
    tmE

    %ticks definitions for dates
    alldates = Qm_ho(tm0:tmE,1);
    dtick =[];
    for f=1:24:length(alldates)
    dv=datevec(alldates(f));
    if dv(3)==1
    dtick=[dtick alldates(f)];
    end
    end
    datevec(dtick)
    %use dtick for ticking the x axis

  2. Claudia Hofemann replied on :

    Hi Loren,
    I am using the Import Wizard, to import .dat files. Afterwards I am splitting them into x, y and z coordinates and I would like to compare this matrices or let’s say the average of the xx with the xx coordinates out of another picture (I have about 100 for each set).

    My files are quite big. The whole .dat file has 1431040×3 double numbers the reshaped about 1377×1040 double values inside.

    It would be perfect if you could give me an advice.

    Claudia

  3. Musumpuka Remmy replied on :

    hello there! am new to matlab and am working with wave files,
    I need help on how i can import a number of wave files into matlab!
    currently am using wavread(’2004-020-074500.wav’) for example
    where “2004-020-074500.wav” is just the filename! now i have more than just a file and i would like to automate the wavreading! kindly help!

    Remmy

  4. Loren replied on :

    Remmy-

    Please see this blog to answer you question on how to process files. In your case, you will need to be careful to assign valid variable names to your data when you read it — it can’t be in the form of your current file names. You might want to look at genvarname.

    –Loren

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


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.

  • Jun: I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
  • Loren: Wow folks- Always lots of interest when there’s a quickie to try out! I will only make 2 general...
  • Loren: Jun- ismember is your friend here: >> [aa,ind] = ismember(Array2,Arra y1) aa = 1 1 1 1 1 1 1 ind = 1 2 1 4 4 3...
  • Dan: I like the first way better than the second way. Combining the arrays into one and running any is nice, although...
  • James Myatt: How about I = (a == 0 | b == 0); a(I) = []; b(I) = [];
  • Tunc: Hello Loren, love your blog because of such inspiring and challenging comments to such ’small’...
  • Pekka Kumpulainen: Here is my tradeoff. I usually want to keep the original variables as they are most probably...
  • Iain: Followup: Of course, to allow NaNs (counting them as non-zero): mask = (a~=0) & (b~=0); The mask says “a...
  • Matt Fig: I would usually go with something like this: y = a&b; x = a(y); y = b(y); But I was surprised to find...
  • kk: c=all([a;b]) a(c) a(b)

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