<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christian's Projects &#187; scientific computing</title>
	<atom:link href="http://www.incasoftware.de/~kamm/projects/index.php/tag/scientific-computing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.incasoftware.de/~kamm/projects</link>
	<description></description>
	<lastBuildDate>Thu, 06 May 2010 17:22:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MATLAB file access</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2007/10/05/matlab-file-access/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2007/10/05/matlab-file-access/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 19:51:57 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[scientific computing]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/index.php/2007/10/05/matlab-file-access/</guid>
		<description><![CDATA[Quite a while ago I was looking at ways to interface between MATLAB and a C++ program for a job at university. In the end I wrote a C++ wrapper for the C API to access MAT files which turned out to be quite useful. Since it&#8217;s small and easy to use, I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>Quite a while ago I was looking at ways to interface between <a href="http://www.mathworks.com/products/matlab">MATLAB</a> and a C++ program for a job at university. In the end I wrote a C++ wrapper for the C API to access <a href="http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format.pdf">MAT files</a> which turned out to be quite useful. Since it&#8217;s small and easy to use, I decided to release it.<br />
<span id="more-5"></span><br />
At first the plan was to make the program into a MEX file (basically a dynamically linked extension program), but I quickly ran into a problem with <a href="http://www.dealii.org">deal II</a> (an excellent, well documented and object-oriented library for solving partial differential equations with adaptive finite elements): MATLAB altered some of the floating point precision settings which lead to an algorithm in deal II running in an infinite loop (the epsilon value was hardcoded at that time and basically became zero). While the bug was promptly fixed, there were other issues and it was decided that embedding the program was not worth the trouble.</p>
<p>Long story short, I needed a way to read and write the MAT files that MATLAB generates and loads with the &#8216;load&#8217; and &#8217;save&#8217; commands. Using the <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/bqoqnz0.html">C API</a> directly seemed cumbersome and error prone, so this C++ wrapper was born.</p>
<p>Download it (4 kB) as <a href="http://www.incasoftware.de/~kamm/projects/wp-content/articles/MATLAB-file-access/matlabfile.tar.bz2">.tar.bz2</a> or as <a href="http://www.incasoftware.de/~kamm/projects/wp-content/articles/MATLAB-file-access/matlabfile.zip">zip</a>.</p>
<p>A basic use case looks like this (also check out test.cc):<br />
<code><br />
#include &quot;matlabfile.h&quot;<br />
using namespace MatlabUtilities;<br />
&nbsp;<br />
File matfile(&quot;some.mat&quot;);<br />
std::vector&lt;double&gt; vec1;<br />
matfile.read&lt;Types::real_vector&gt;(&quot;v1&quot;, vec1);<br />
&nbsp;<br />
double[100] vec2;<br />
matfile.read&lt;Types::fixed_length_real_vector&lt;100&gt; &gt;(&quot;v2&quot;, vec2);<br />
&nbsp;<br />
matfile.write&lt;Types::string&gt;(&quot;hw&quot;, &quot;Hello World!&quot;);<br />
</code></p>
<p>Not all MATLAB types are implemented (check Types namespace in matlabaccessors.h for a list), in particular real matrices with a column and row length different from one are missing for two reasons: I didn&#8217;t need them for the job and you probably have your own preferred matrix type anyway. But don&#8217;t worry, the template-design makes it easy to add these missing features. What needs to be implemented is<br />
<code><br />
struct real_matrix {};<br />
&nbsp;<br />
template &lt;&gt;<br />
struct Reader&lt;real_matrix, MyMatrix&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static void read(mxArray* var, MyMatrix&amp; result);<br />
}<br />
&nbsp;<br />
template &lt;&gt;<br />
struct Writer&lt;real_matrix, MyMatrix&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static mxArray* write(MyMatrix&amp; source);<br />
}<br />
&nbsp;<br />
template &lt;&gt;<br />
struct TypeCheck&lt;real_matrix&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static bool typeCheck(mxArray* var);<br />
}<br />
</code></p>
<p>where the &#8216;read&#8217; function reads data from the mxArray into result, &#8216;write&#8217; returns a new mxArray filled with source&#8217;s data and &#8216;typeCheck&#8217; returns true if the mxArray contains a real_matrix. With these template specializations in place you can use the new code like this:<br />
<code><br />
MyMatrix mat;<br />
matfile.read&lt;real_matrix&gt;(&quot;somevarname&quot;, mat);<br />
matfile.write&lt;real_matrix&gt;(&quot;someother&quot;, mat);<br />
</code><br />
Similarly, you can add a new reader to say &#8216;real_vector&#8217; to accomodate for your own vector type.</p>
<p>The code snippet only contains code for reading and writing data. Other management functionality, like listing all fields in a MAT file is not implemented. I hope it will nevertheless be useful for someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2007/10/05/matlab-file-access/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
