<?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>憂藍夢境‧部落格 &#187; MATLAB</title>
	<atom:link href="http://blog.linym.net/archives/tag/matlab/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.linym.net</link>
	<description>我的學習心得、筆記</description>
	<lastBuildDate>Fri, 09 Dec 2011 12:33:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>使用 C# 呼叫 MATLAB Function</title>
		<link>http://blog.linym.net/archives/564</link>
		<comments>http://blog.linym.net/archives/564#comments</comments>
		<pubDate>Thu, 10 Sep 2009 07:49:21 +0000</pubDate>
		<dc:creator>lym520</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[程式筆記]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.linym.net/?p=564</guid>
		<description><![CDATA[最近在寫一些演算法的程式，經常要在一堆程式和檔案切換來切換去的，跑完數據還要開 MATLAB 畫圖，一直重複這些動作實在是很麻煩，所以把它寫成 Windows Form 用滑鼠點一點就 OK 了，其中繪圖部份就要利用 C# 去 call MATLAB 來作，查了許多資料，發現有很多種方法，但比較簡單方便的應該是用 MATLAB Builder NE for .NET 將 MATLAB 程式編譯成 .NET 可執行的元件。 1. 首先準備好你要 compiler 的 M-file，測試的 M-file 如下(由檔案讀取 x, y 數據曲線圖)： function rmse(file) data = load(file); x = data(:,1); y = data(:,2); plot(x,y,&#039;LineWidth&#039;,1); xlabel(&#039;Iteration&#039;); ylabel(&#039;RMS error&#039;); 2. 開啟 MATLAB 並在 command windows 輸入：deploytool [...]]]></description>
			<content:encoded><![CDATA[<p>最近在寫一些演算法的程式，經常要在一堆程式和檔案切換來切換去的，跑完數據還要開 MATLAB 畫圖，一直重複這些動作實在是很麻煩，所以把它寫成 Windows Form 用滑鼠點一點就 OK 了，其中繪圖部份就要利用 C# 去 call MATLAB 來作，查了許多資料，發現有很多種方法，但比較簡單方便的應該是用 <a href="http://www.mathworks.com/products/netbuilder/" target="_blank">MATLAB Builder NE for .NET</a> 將 MATLAB 程式編譯成 .NET 可執行的元件。</p>
<p>1. 首先準備好你要 compiler 的 M-file，測試的 M-file 如下(由檔案讀取 x, y 數據曲線圖)：</p>
<pre title="code" class="cpp">

function rmse(file)

data = load(file);
x = data(:,1);
y = data(:,2);

plot(x,y,&#039;LineWidth&#039;,1);

xlabel(&#039;Iteration&#039;);
ylabel(&#039;RMS error&#039;);
</pre>
<p>2. 開啟 MATLAB 並在 command windows 輸入：<strong>deploytool</strong><br />
    新建一個 project，名稱為 rmse。<br />
    <a href="http://blog.linym.net/wp-content/uploads/2009/09/01.JPG"><img src="http://blog.linym.net/wp-content/uploads/2009/09/01.JPG" alt="New Project" title="New Project" width="646" height="451" class="alignnone size-full wp-image-567" /></a><br />
    選擇 .NET component<br />
    <a href="http://blog.linym.net/wp-content/uploads/2009/09/2.JPG"><img src="http://blog.linym.net/wp-content/uploads/2009/09/2.JPG" alt=".NET component" title=".NET component" width="510" height="510" class="alignnone size-full wp-image-571" /></a><br />
    將 M-file 加入，然後按<strong> Build the project</strong>，編譯完成會產生 rmse.dll<br />
<a href="http://blog.linym.net/wp-content/uploads/2009/09/3.JPG"><img src="http://blog.linym.net/wp-content/uploads/2009/09/3.JPG" alt="Add file" title="Add file" width="646" height="451" class="alignnone size-full wp-image-574" /></a></p>
<p>3. 將 .dll 加入 Visual Studio 參考(Reference)<br />
   在方案總管右鍵選「加入參考」>「瀏覽」，將剛剛編譯完成的 .dll 以及 MATLAB 安裝目錄底下的 toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll 加入。</p>
<p>4. 加入 Namespace<br />
using MathWorks.MATLAB.NET.Arrays;<br />
using rmse;</p>
<p>5. 使用方式<br />
    Rmse demo = new Rmse();<br />
    MWArray fileName = @"rmse.txt";<br />
    demo.rmse(fileName);</p>
<p>※ <a href='http://blog.linym.net/wp-content/uploads/2009/09/CSharp_Matlab.rar'>demo 範例原始碼下載</a></p>
<p>※ 如果想在沒安裝 MATLAB 的電腦上執行，需安裝 MATLAB Compiler Runtime (MCR)，檔案在安裝目錄的 toolbox\compiler\deploy\win32\MCRInstaller.exe，大小約 150MB</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.linym.net/archives/564/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

