| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | 
|---|
| 2 | <html xmlns="http://www.w3.org/1999/xhtml"> | 
|---|
| 3 | <head> | 
|---|
| 4 | <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> | 
|---|
| 5 | <title>mixpp: Writing and reading data from files</title> | 
|---|
| 6 | <link href="tabs.css" rel="stylesheet" type="text/css"/> | 
|---|
| 7 | <link href="doxygen.css" rel="stylesheet" type="text/css"/> | 
|---|
| 8 | </head> | 
|---|
| 9 | <body> | 
|---|
| 10 | <!-- Generated by Doxygen 1.6.1 --> | 
|---|
| 11 | <script type="text/javascript"> | 
|---|
| 12 | <!-- | 
|---|
| 13 | function changeDisplayState (e){ | 
|---|
| 14 |   var num=this.id.replace(/[^[0-9]/g,''); | 
|---|
| 15 |   var button=this.firstChild; | 
|---|
| 16 |   var sectionDiv=document.getElementById('dynsection'+num); | 
|---|
| 17 |   if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){ | 
|---|
| 18 |     sectionDiv.style.display='block'; | 
|---|
| 19 |     button.src='open.gif'; | 
|---|
| 20 |   }else{ | 
|---|
| 21 |     sectionDiv.style.display='none'; | 
|---|
| 22 |     button.src='closed.gif'; | 
|---|
| 23 |   } | 
|---|
| 24 | } | 
|---|
| 25 | function initDynSections(){ | 
|---|
| 26 |   var divs=document.getElementsByTagName('div'); | 
|---|
| 27 |   var sectionCounter=1; | 
|---|
| 28 |   for(var i=0;i<divs.length-1;i++){ | 
|---|
| 29 |     if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){ | 
|---|
| 30 |       var header=divs[i]; | 
|---|
| 31 |       var section=divs[i+1]; | 
|---|
| 32 |       var button=header.firstChild; | 
|---|
| 33 |       if (button!='IMG'){ | 
|---|
| 34 |         divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild); | 
|---|
| 35 |         button=document.createElement('img'); | 
|---|
| 36 |         divs[i].insertBefore(button,divs[i].firstChild); | 
|---|
| 37 |       } | 
|---|
| 38 |       header.style.cursor='pointer'; | 
|---|
| 39 |       header.onclick=changeDisplayState; | 
|---|
| 40 |       header.id='dynheader'+sectionCounter; | 
|---|
| 41 |       button.src='closed.gif'; | 
|---|
| 42 |       section.id='dynsection'+sectionCounter; | 
|---|
| 43 |       section.style.display='none'; | 
|---|
| 44 |       section.style.marginLeft='14px'; | 
|---|
| 45 |       sectionCounter++; | 
|---|
| 46 |     } | 
|---|
| 47 |   } | 
|---|
| 48 | } | 
|---|
| 49 | window.onload = initDynSections; | 
|---|
| 50 | --> | 
|---|
| 51 | </script> | 
|---|
| 52 | <div class="navigation" id="top"> | 
|---|
| 53 |   <div class="tabs"> | 
|---|
| 54 |     <ul> | 
|---|
| 55 |       <li><a href="main.html"><span>Main Page</span></a></li> | 
|---|
| 56 |       <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> | 
|---|
| 57 |       <li><a href="annotated.html"><span>Classes</span></a></li> | 
|---|
| 58 |       <li><a href="files.html"><span>Files</span></a></li> | 
|---|
| 59 |     </ul> | 
|---|
| 60 |   </div> | 
|---|
| 61 |   <div class="navpath"><a class="el" href="manual.html">User Manual</a> | 
|---|
| 62 |   </div> | 
|---|
| 63 | </div> | 
|---|
| 64 | <div class="contents"> | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | <h1><a class="anchor" id="itfile">Writing and reading data from files </a></h1><p>Here we will use the <code>it_file</code> class to store some data. The program <code>write_it_file.cpp</code> looks as follows:</p> | 
|---|
| 68 | <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <itpp/itcomm.h></span> | 
|---|
| 69 |  | 
|---|
| 70 | <span class="keyword">using namespace </span>itpp; | 
|---|
| 71 |  | 
|---|
| 72 | <span class="keywordtype">int</span> main() { | 
|---|
| 73 |         <span class="comment">// Declare the it_file class</span> | 
|---|
| 74 |         it_file ff; | 
|---|
| 75 |  | 
|---|
| 76 |         <span class="comment">// Open a file with the name "it_file_test.it"</span> | 
|---|
| 77 |         ff.open ( <span class="stringliteral">"it_file_test.it"</span> ); | 
|---|
| 78 |  | 
|---|
| 79 |         <span class="comment">// Create some data to put into the file</span> | 
|---|
| 80 |         vec a = linspace ( 1, 20, 20 ); | 
|---|
| 81 |  | 
|---|
| 82 |         <span class="comment">// Put the variable a into the file. The Name("a") tells the file class</span> | 
|---|
| 83 |         <span class="comment">// that the next variable shall be named "a".</span> | 
|---|
| 84 |         ff << Name ( <span class="stringliteral">"a"</span> ) << a; | 
|---|
| 85 |  | 
|---|
| 86 |         <span class="comment">// Force the file to be written to disc. This is useful when performing</span> | 
|---|
| 87 |         <span class="comment">// iterations and ensures that the information is not stored in any cache</span> | 
|---|
| 88 |         <span class="comment">// memory. In this simple example it is not necessary to flush the file.</span> | 
|---|
| 89 |         ff.flush(); | 
|---|
| 90 |  | 
|---|
| 91 |         <span class="comment">// Close the file</span> | 
|---|
| 92 |         ff.close(); | 
|---|
| 93 |  | 
|---|
| 94 |         <span class="comment">// Exit program</span> | 
|---|
| 95 |         <span class="keywordflow">return</span> 0; | 
|---|
| 96 | } | 
|---|
| 97 | </pre></div><p>When you run this program you will obtain a file called <code>it_file_test.it</code> in your current directory. You can read the file into Matlab/Octave to view the data by using the following commands:</p> | 
|---|
| 98 | <div class="fragment"><pre class="fragment">itload(<span class="stringliteral">'it_file_test.it'</span>) | 
|---|
| 99 | figure(1); clf; | 
|---|
| 100 | plot(a) | 
|---|
| 101 | </pre></div><p>Note: Make sure that <code>$PREFIX/share/itpp</code> is in your Matlab/Octave path and that you run the code above from the directory where <code>it_file_test.it</code> is located (<code>$PREFIX</code> is the IT++ installation prefix; <code>/usr/local</code> by default).</p> | 
|---|
| 102 | <p>The IT++ program <code>read_it_file.cpp</code> that reads the file and prints its content can look like this:</p> | 
|---|
| 103 | <div class="fragment"><pre class="fragment"></pre></div><p>Here is the output of the program:</p> | 
|---|
| 104 | <div class="fragment"><pre class="fragment"> | 
|---|
| 105 | a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] | 
|---|
| 106 | </pre></div> </div> | 
|---|
| 107 | <hr size="1"/><address style="text-align: right;"><small>Generated on Sun Aug 30 22:10:50 2009 for mixpp by  | 
|---|
| 108 | <a href="http://www.doxygen.org/index.html"> | 
|---|
| 109 | <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> | 
|---|
| 110 | </body> | 
|---|
| 111 | </html> | 
|---|