{"id":1502,"date":"2009-07-10T11:51:47","date_gmt":"2009-07-10T10:51:47","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=1502"},"modified":"2009-07-10T11:51:47","modified_gmt":"2009-07-10T10:51:47","slug":"converting-from-cell-arrays-to-matrices-in-matlab","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=1502","title":{"rendered":"Converting from cell arrays to matrices in MATLAB"},"content":{"rendered":"<p>I had a query from a MATLAB user the other day and I thought I would share it with the world just in case it turned out to be useful to someone.\u00a0 She had some data in a cell array that appeared as follows<\/p>\n<pre>data =\r\n\r\n    '-0.000252594'\r\n    '-0.788638'\r\n    '-1.14636'\r\n    '-1.15374'\r\n    '-1.15474'<\/pre>\n<p>I&#8217;m not sure how she ended up with her data in this form but that&#8217;s not important here.  What is important is that she couldn&#8217;t calculate with it in all the ways she was used to so what she asked was &#8216;<strong>how do I convert this cell array to a matrix?<\/strong>&#8216;<\/p>\n<p>It turns out that this is relatively easy to do since this particular cell array has a very simple form.\u00a0 If you want to follow along then you can create an identical cell array as follows<\/p>\n<pre>\r\ndata={'-0.000252594'; '-0.788638'; '-1.14636' ;'-1.15374' ;'-1.15474'}\r\n\r\ndata =\r\n\r\n    '-0.000252594'\r\n    '-0.788638'\r\n    '-1.14636'\r\n    '-1.15374'\r\n    '-1.15474'\r\n<\/pre>\n<p>Let&#8217;s check that this really is a cell array using the <strong>iscell()<\/strong> function<\/p>\n<pre>\r\niscell(data)\r\n\r\nans =\r\n     1\r\n<\/pre>\n<p>Looks good so far.  So, to convert this to a matrix all you need to do is<\/p>\n<pre>\r\nmatdata=cellfun(@str2num,data)\r\n\r\nmatdata =\r\n   -0.0003\r\n   -0.7886\r\n   -1.1464\r\n   -1.1537\r\n   -1.1547\r\n<\/pre>\n<p>the variable <strong>matdata<\/strong> is a standard MATLAB matrix and to prove it I&#8217;ll add 1 to all of the elements in the usual fashion<\/p>\n<pre>\r\nmatdata+1\r\n\r\nans =\r\n    0.9997\r\n    0.2114\r\n   -0.1464\r\n   -0.1537\r\n   -0.1547\r\n<\/pre>\n<p>That&#8217;s it!  If only all queries were that simple :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a query from a MATLAB user the other day and I thought I would share it with the world just in case it turned out to be useful to someone.\u00a0 She had some data in a cell array that appeared as follows data = &#8216;-0.000252594&#8217; &#8216;-0.788638&#8217; &#8216;-1.14636&#8217; &#8216;-1.15374&#8217; &#8216;-1.15474&#8242; I&#8217;m not sure how [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-1502","post","type-post","status-publish","format-standard","hentry","category-matlab"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-oe","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1502"}],"version-history":[{"count":4,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1502\/revisions"}],"predecessor-version":[{"id":1506,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1502\/revisions\/1506"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}