Loren on the Art of MATLAB

Turn ideas into MATLAB

Code Generation from Import Wizard 4

Posted by Loren Shure,

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

Note

Comments are closed.

4 CommentsOldest to Newest

Maik replied on : 1 of 4
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
Claudia Hofemann replied on : 2 of 4
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 1431040x3 double numbers the reshaped about 1377x1040 double values inside. It would be perfect if you could give me an advice. Claudia
Musumpuka Remmy replied on : 3 of 4
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
Loren replied on : 4 of 4
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