<?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>Tayyab Bin Tariq &#187; Matlab Image</title>
	<atom:link href="http://tayyab.xenoglaux-solutions.com/tag/matlab-image/feed/" rel="self" type="application/rss+xml" />
	<link>http://tayyab.xenoglaux-solutions.com</link>
	<description>I like sharing what i know</description>
	<lastBuildDate>Fri, 21 May 2010 20:59:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Matlab GUI: Handling User Data and Images</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 17:00:35 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Application Data]]></category>
		<category><![CDATA[getuserdata]]></category>
		<category><![CDATA[GUIDE]]></category>
		<category><![CDATA[imread]]></category>
		<category><![CDATA[imshow]]></category>
		<category><![CDATA[Matlab GUI]]></category>
		<category><![CDATA[Matlab GUI: Handling User Data and Images]]></category>
		<category><![CDATA[Matlab Image]]></category>
		<category><![CDATA[Matlab User Data]]></category>
		<category><![CDATA[setuserdata]]></category>
		<category><![CDATA[User Data]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/</guid>
		<description><![CDATA[ 
It is often an issue to handle user defined data when working with Matlab GUI. In this tutorial I show how to share user data among functions and how to handle Images.
&#160;
&#160;
&#160;
 
I assume basic knowledge of Matlab GUI. (Please see the tutorial on GUI basics).
We create a simple GUI where a user can [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/matlab1.jpg"><img title="matlab" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 10px 0px; border-right-width: 0px" height="184" alt="matlab" src="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/matlab-thumb1.jpg" width="244" align="left" border="0" /></a> </p>
<p>It is often an issue to handle user defined data when working with Matlab GUI. In this tutorial I show how to share user data among functions and how to handle Images.</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p> <span id="more-34"></span>
<p>I assume basic knowledge of Matlab GUI. (Please see the tutorial on <a href="http://tayyab.xenoglaux-solutions.com/2009/04/06/creating-matlab-gui-the-basics/" target="_blank">GUI basics</a>).</p>
<p>We create a simple GUI where a user can select an image and then press a button to display it.</p>
<p><a href="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/image6.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="379" alt="image" src="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/image-thumb6.png" width="644" border="0" /></a> </p>
<p>First create a simple GUI with an edit box and two push buttons.</p>
<p><a href="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/image7.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="316" alt="image" src="http://tayyab.xenoglaux-solutions.com/wp-content/uploads/2009/04/image-thumb7.png" width="644" border="0" /></a> </p>
<p>The plan of action is to allow the user to select a file when he/she presses the ‘Browse’ button, and show the image when he ‘Show Image’ button is pressed.</p>
<h3>Reading Images and Setting User Data</h3>
<p>In the ‘Browse’ button call back i add the following code.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> % get a file to read </pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> [FileName, FilePath] = uigetfile({ <span style="color: #006080">'*.jpg'</span>; <span style="color: #006080">'*.jpeg'</span>; <span style="color: #006080">'*.bmp'</span>; <span style="color: #006080">'*.gif'</span>; <span style="color: #006080">'*.tiff'</span>; <span style="color: #006080">'*.png'</span> }, <span style="color: #006080">'Select an Image...'</span>);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span> % set the file name and path to the edit box as well</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span> set(handles.editFilePath, <span style="color: #006080">'String'</span>, [FilePath, FileName]);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span> % read the image</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum6" style="color: #606060">   6:</span> I = imread([FilePath, FileName]);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum7" style="color: #606060">   7:</span> % set the image <span style="color: #0000ff">in</span> the user data, <span style="color: #0000ff">if</span> this is not done, the image would be</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum8" style="color: #606060">   8:</span> % lost</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum9" style="color: #606060">   9:</span> setappdata(JigsawPuzzle, <span style="color: #006080">'MyImage'</span>, I);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum10" style="color: #606060">  10:</span> % set the file name and path <span style="color: #0000ff">in</span> the user data</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum11" style="color: #606060">  11:</span> setappdata(JigsawPuzzle, <span style="color: #006080">'FileNameAndPath'</span>, [FilePath, FileName]);</pre>
<p><!--CRLF--></div>
</div>
<p>The last two lines are the most important and need explanation.</p>
<p><font face="Courier"><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/setappdata.html&amp;http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/f5-998352.html" target="_blank">setappdata</a>(JigsawPuzzle, &#8216;MyImage&#8217;, I);</font></p>
<p>The first argument of the ‘setappdata’ is the name of a figure object or any object contained within it that will hold the data as its data object, you could use any control but I use the figure itself to assert the global scope of the variables. The second argument is the name of the data variable. The third argument is the data itself.</p>
<p>The last line sets another variable with filename and path, concatenated together.</p>
<p>PS: You might want to read about uigetfile (Use help uigetfile in Matlab).</p>
<p>I have also added similar code to the edit box.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 106.96%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; height: 123px; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> FilePath = get(handles.editFilePath, <span style="color: #006080">'String'</span>);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> set(handles.editFilePath, <span style="color: #006080">'String'</span>, FilePath);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum3" style="color: #606060">   3:</span> I = imread(FilePath);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum4" style="color: #606060">   4:</span> setappdata(JigsawPuzzle, <span style="color: #006080">'MyImage'</span>, I);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum5" style="color: #606060">   5:</span> setappdata(JigsawPuzzle, <span style="color: #006080">'FileNameAndPath'</span>, FilePath);</pre>
<p><!--CRLF--></div>
</div>
<h3>Retrieving User Data and Showing the image</h3>
<p>This part is rather simpler. I add the following code to the show image button.</p>
<div id="codeSnippetWrapper" style="border-right: silver 1px solid; padding-right: 4px; border-top: silver 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: silver 1px solid; width: 97.5%; cursor: text; direction: ltr; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4; text-align: left">
<div id="codeSnippet" style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; text-align: left; border-bottom-style: none"><span id="lnum1" style="color: #606060">   1:</span> Image = getappdata(JigsawPuzzle, <span style="color: #006080">'MyImage'</span>);</pre>
<p><!--CRLF--></p>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; direction: ltr; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; text-align: left; border-bottom-style: none"><span id="lnum2" style="color: #606060">   2:</span> figure, imshow(Image);</pre>
<p><!--CRLF--></div>
</div>
<p>The&#160; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/getappdata.html" target="_blank">getappdata</a> is similar to get function used to retrieve values for control properties.</p>
<p>Please feel free to leave comments and questions.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/04/06/matlab-gui-handling-user-data-and-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

