{"id":57,"date":"2006-10-05T15:36:09","date_gmt":"2006-10-05T20:36:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=57"},"modified":"2017-09-24T19:21:57","modified_gmt":"2017-09-25T00:21:57","slug":"code-generation-from-import-wizard","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/05\/code-generation-from-import-wizard\/","title":{"rendered":"Code Generation from Import Wizard"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Some colleagues and I were talking to some customers recently at a seminar and asked them what some of their favorite <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html\">new features<\/a> are.  One of the top ones is being able to generate code from the Import Wizard.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What is the Import Wizard?<\/a><\/li>\r\n         <li><a href=\"#2\">Working with External Data<\/a><\/li>\r\n         <li><a href=\"#3\">Using the Import Wizard<\/a><\/li>\r\n         <li><a href=\"#7\">Using the Generated Code<\/a><\/li>\r\n         <li><a href=\"#10\">What Features Work Well for You?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What is the Import Wizard?<a name=\"1\"><\/a><\/h3>\r\n   <p>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\r\n      some projects, you may have many data files with consistent formats, but you don't necessarily know the format until you look\r\n      at a representative file. With the Import Wizard, you can read in the first data set the way you'd like and generate code\r\n      for the process.  Now you can use the generated M-file to automate importing additional files for future processing.\r\n   <\/p>\r\n   <h3>Working with External Data<a name=\"2\"><\/a><\/h3>\r\n   <p>In a <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=48\">recent post<\/a>, I converted a collection of <tt>.mat<\/tt>-files containing audio data to a set of  <tt>.wav<\/tt> files. Since MATLAB already contains <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/audioread.html\"><tt>wavread<\/tt><\/a> for reading in wav-files, we can automate reading in this sort of data already.  But suppose instead we had a set of spreadsheets,\r\n      each containing two columns of data, with headers <tt>time<\/tt> and <tt>temperature<\/tt>. We could certainly use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/xlsread.html\"><tt>xlsread<\/tt><\/a>, but it would be a bit of work and programming to get the data into two variables named <tt>time<\/tt> and <tt>temperature<\/tt>.\r\n   <\/p>\r\n   <h3>Using the Import Wizard<a name=\"3\"><\/a><\/h3>\r\n   <p>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\r\n      like for the first file: <tt>timetempPHX.xls<\/tt>.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/57\/uiimport1.png\"> <\/p>\r\n   <p>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\r\n      into variables, with the column names becoming the variable names in MATLAB.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/57\/uiimport2.png\"> <\/p>\r\n   <p>Now look in the lower right corner and you will see a check box that I now select to generate code for this dataset.<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/57\/uiimport3.png\"> <\/p>\r\n   <p>After the code is generated, I save the file as <tt>importfileTimeTemp.m<\/tt>.\r\n   <\/p>\r\n   <h3>Using the Generated Code<a name=\"7\"><\/a><\/h3>\r\n   <p>Now I can read in these files in a more automated way, allowing me to script some analysis.  We'll load two such datasets\r\n      and plot them on the same figure to compare fabricated monthly average high temperatures for a year in Boston and Phoenix.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">importfileTimeTemp <span style=\"color: #A020F0\">timetempPHX.xls<\/span><\/pre><p>Convert months into the correct format for the year 2005.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">date = datenum(2005,month,1);\r\nticks = cumsum([min(date) 31 28 31 30 31 30 31 31 30 31 30]);<\/pre><p>Plot the first data set, set x-axis ticks correctly, then get and plot the second set of data.  Finally, add a legend and\r\n      title.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(date,temperature,<span style=\"color: #A020F0\">'r*'<\/span>)\r\nset(gca,<span style=\"color: #A020F0\">'Xtick'<\/span>,ticks)\r\ndatetick(<span style=\"color: #A020F0\">'x'<\/span>,3,<span style=\"color: #A020F0\">'keepticks'<\/span>)\r\nimportfileTimeTemp <span style=\"color: #A020F0\">timetempBOS.xls<\/span>\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(date,temperature,<span style=\"color: #A020F0\">'bo'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Guesses for average daily high temperatures'<\/span>)\r\nlegend({<span style=\"color: #A020F0\">'Phoenix'<\/span> <span style=\"color: #A020F0\">'Boston'<\/span>},<span style=\"color: #A020F0\">'location'<\/span>,<span style=\"color: #A020F0\">'northwest'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/57\/impWizCode_01.png\"> <h3>What Features Work Well for You?<a name=\"10\"><\/a><\/h3>\r\n   <p>In the past couple of blog articles, I have tried to show you some of the features new to <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html?s_cid=HP_RH_2006b\">R2006b<\/a>. Which features solve a problem for you?  <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/05\/code-generation-from-import-wizard\/#respond\">Let me know.<\/a><\/p>\r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Code Generation from Import Wizard\r\n% Some colleagues and I were talking to some customers recently at a\r\n% seminar and asked them what some of their favorite \r\n% <https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html new features>\r\n% are.  One of the top ones is being able to generate code from the Import\r\n% Wizard.\r\n%% What is the Import Wizard?\r\n% Some of you may ask what the\r\n% Import Wizard\r\n% is.  It's a graphical tool that allows you to load in data to MATLAB\r\n% without knowing the explicit format of the data.  For some projects, you\r\n% may have many data files with consistent formats, but you don't\r\n% necessarily know the format until you look at a representative file.\r\n% With the Import Wizard, you can read in the first data set the way you'd\r\n% like and generate code for the process.  Now you can use the generated\r\n% M-file to automate importing additional files for future processing.\r\n%% Working with External Data\r\n% In a <https:\/\/blogs.mathworks.com\/loren\/?p=48 recent post>, I converted a\r\n% collection of |.mat|-files containing audio data to a set of  |.wav|\r\n% files. Since MATLAB already contains \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/audioread.html |wavread|>\r\n% for reading in wav-files, we can automate reading in this sort of data\r\n% already.  But suppose instead we had a set of spreadsheets, each\r\n% containing two columns of data, with headers |time| and |temperature|.\r\n% We could certainly use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/xlsread.html |xlsread|>, \r\n% but it would be a bit of work and programming to get the data into two\r\n% variables named |time| and |temperature|.  \r\n%% Using the Import Wizard\r\n% Instead, I can use the import wizard to help figure out how I want to\r\n% represent the data.  Let's see what the wizard looks like for the first\r\n% file: |timetempPHX.xls|.\r\n%\r\n% <<uiimport1.png>>\r\n%%\r\n% You can see that I can read the data in, with column names and other text\r\n% data placed into cells.  I can also read the data into variables, with\r\n% the column names becoming the variable names in MATLAB.\r\n%\r\n% <<uiimport2.png>>\r\n%\r\n%%\r\n% Now look in the lower right corner and you will see a check box that I\r\n% now select to generate code for this dataset.\r\n%\r\n% <<uiimport3.png>>\r\n%\r\n%%\r\n% After the code is generated, I save the file as |importfileTimeTemp.m|.\r\n%% Using the Generated Code\r\n% Now I can read in these files in a more automated way, allowing me to\r\n% script some analysis.  We'll load two such datasets and plot them on the\r\n% same figure to compare fabricated monthly average high temperatures for a\r\n% year in Boston and Phoenix.\r\nimportfileTimeTemp timetempPHX.xls\r\n%%\r\n% Convert months into the correct format for the year 2005.\r\ndate = datenum(2005,month,1);\r\nticks = cumsum([min(date) 31 28 31 30 31 30 31 31 30 31 30]);\r\n%%\r\n% Plot the first data set, set x-axis ticks correctly, then get and plot\r\n% the second set of data.  Finally, add a legend and title.\r\nplot(date,temperature,'r*')\r\nset(gca,'Xtick',ticks)\r\ndatetick('x',3,'keepticks')\r\nimportfileTimeTemp timetempBOS.xls\r\nhold on\r\nplot(date,temperature,'bo')\r\ntitle('Guesses for average daily high temperatures')\r\nlegend({'Phoenix' 'Boston'},'location','northwest')\r\nhold off\r\n%% What Features Work Well for You?\r\n% In the past couple of blog articles, I have tried to show you some of the\r\n% features new to\r\n% <https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html?s_cid=HP_RH_2006b R2006b>.\r\n% Which features solve a problem for you?  <?p=56#respond Let me know.>\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/10\/05\/code-generation-from-import-wizard\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,13],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/57"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":2443,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/57\/revisions\/2443"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}