{"id":329,"date":"2013-04-13T15:43:00","date_gmt":"2013-04-13T15:43:00","guid":{"rendered":"http:\/\/ahay.org\/blog\/?p=329"},"modified":"2015-08-04T23:51:32","modified_gmt":"2015-08-04T23:51:32","slug":"how-can-i-read-and-write-rsf-files-in-matlab","status":"publish","type":"post","link":"https:\/\/ahay.org\/blog\/2013\/04\/13\/how-can-i-read-and-write-rsf-files-in-matlab\/","title":{"rendered":"How can I read and write RSF files in MATLAB?"},"content":{"rendered":"<ul>\n<li>The most straightforward way is to install the <a href=\"\/wiki\/Guide_to_madagascar_API#MATLAB_interface\">MATLAB interface to Madagascar<\/a>. When installing Madagascar, run\n<div class=\"code-box\"><div class=\"code-title\"><i class=\"fa fa-code\"><\/i> <div class=\"pull-right\"><a href=\"#\" class=\"btn btn-default btn-xs toggle-code\" data-toggle=\"tooltip\" title=\"Toggle code\"><i class=\"fa fa-toggle-up\"><\/i><\/a><\/div><\/div><pre >.\/configure API=matlab<\/pre><\/div>\n<p> The configure script will try to find and test matlab and <a href=\"http:\/\/www.mathworks.com\/help\/matlab\/ref\/mex.html\">mex<\/a> executibles on your system. If they are not in your PATH, you can specify them with <\/p>\n<div class=\"code-box\"><div class=\"code-title\"><i class=\"fa fa-code\"><\/i> <div class=\"pull-right\"><a href=\"#\" class=\"btn btn-default btn-xs toggle-code\" data-toggle=\"tooltip\" title=\"Toggle code\"><i class=\"fa fa-toggle-up\"><\/i><\/a><\/div><\/div><pre >.\/configure API=matlab MATLAB=\/path\/to\/matlab MEX=\/path\/to\/mex<\/pre><\/div>\n<p> Install Madagascar as usual, set <a href=\"http:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/what-is-the-matlab-search-path.html\">MATLAB path<\/a> to <strong>$RSFROOT\/lib<\/strong>, and you will able to read and write RSF files from MATLAB using <strong>rsf_read<\/strong>, <strong>rsf_write<\/strong>, and other functions from the <a href=\"\/wiki\/Guide_to_madagascar_API#MATLAB_interface\">Madagascar interface<\/a>.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/RSF\/book\/rsf\/rsf\/plplot\/Fig\/logo.png\" width=\"389\" height=\"310\" alt=\"\" \/><\/li>\n<li>Alternatively, you can try reading binary data using MATLAB functions, as in the following example <div class=\"code-box\"><div class=\"code-title\"><i class=\"fa fa-code\"><\/i> <div class=\"pull-right\"><a href=\"#\" class=\"btn btn-default btn-xs toggle-code\" data-toggle=\"tooltip\" title=\"Toggle code\"><i class=\"fa fa-toggle-up\"><\/i><\/a><\/div><\/div><pre ><br \/>\n<I><FONT COLOR=\"#B22222\">% get in=, n1=, and n2= parameters from file.rsf<br \/>\n<\/FONT><\/I><B><FONT COLOR=\"#228B22\">[<\/FONT><\/B>stat,in<B><FONT COLOR=\"#228B22\">]<\/FONT><\/B> = unix(<B><FONT COLOR=\"#BC8F8F\">&#8216;sfget in parform=n &lt; file.rsf&#8217;<\/FONT><\/B>)<br \/>\nin = strtrim(in)<br \/>\n<B><FONT COLOR=\"#228B22\">[<\/FONT><\/B>stat,n1<B><FONT COLOR=\"#228B22\">]<\/FONT><\/B> = unix(<B><FONT COLOR=\"#BC8F8F\">&#8216;sfget n1 parform=n &lt; file.rsf&#8217;<\/FONT><\/B>)<br \/>\nn1 = <B><FONT COLOR=\"#228B22\">str2num<\/FONT><\/B>(n1)<br \/>\n<B><FONT COLOR=\"#228B22\">[<\/FONT><\/B>stat,n2<B><FONT COLOR=\"#228B22\">]<\/FONT><\/B> = unix(<B><FONT COLOR=\"#BC8F8F\">&#8216;sfget n2 parform=n &lt; file.rsf&#8217;<\/FONT><\/B>)<br \/>\nn2 = <B><FONT COLOR=\"#228B22\">str2num<\/FONT><\/B>(n2)<br \/>\n<I><FONT COLOR=\"#B22222\">% read binary data<br \/>\n<\/FONT><\/I><B><FONT COLOR=\"#228B22\">fid<\/FONT><\/B> = <B><FONT COLOR=\"#228B22\">fopen<\/FONT><\/B>(in,<B><FONT COLOR=\"#BC8F8F\">&#8216;rb&#8217;<\/FONT><\/B>)<br \/>\ndata = <B><FONT COLOR=\"#228B22\">fread<\/FONT><\/B>(<B><FONT COLOR=\"#228B22\">fid<\/FONT><\/B>,n1*n2,<B><FONT COLOR=\"#BC8F8F\">&#8216;float32&#8217;<\/FONT><\/B>);<br \/>\n<I><FONT COLOR=\"#B22222\">% reshape to 2-D matrix<br \/>\n<\/FONT><\/I>data = <B><FONT COLOR=\"#228B22\">reshape<\/FONT><\/B>(data,n1,n2);<\/PRE><\/li>\n<li>An even better alternative is to abandone MATLAB in favor of free software, such as <a href=\"http:\/\/www.gnu.org\/software\/octave\/\">GNU Octave<\/a>, Python with <a href=\"http:\/\/www.numpy.org\/\">NumPy<\/a>, <a href=\"http:\/\/www.sagemath.org\/\">Sage<\/a>, etc. A <a href=\"\/wiki\/Conferences#Austin_2012_.28SciPy.29\">Python interface to Madagascar<\/a> is installed by default.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The most straightforward way is to install the MATLAB interface to Madagascar. When installing Madagascar, run .\/configure API=matlab The configure script will try to find and test matlab and mex executibles on your system. If they are not in your PATH, you can specify them with .\/configure API=matlab MATLAB=\/path\/to\/matlab MEX=\/path\/to\/mex Install Madagascar as usual, set [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-faq"],"_links":{"self":[{"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":1,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":586,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/586"}],"wp:attachment":[{"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahay.org\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}