<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Advanced: loading files using their names for variable names</title>
	<atom:link href="http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/</link>
	<description>Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.</description>
	<lastBuildDate>Fri, 10 Feb 2012 20:31:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: liz</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2870</link>
		<dc:creator>liz</dc:creator>
		<pubDate>Fri, 11 Nov 2011 21:25:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2870</guid>
		<description>Hi, 

adding onto the discussion, I wanted to do the same thing as Jason, but instead of just doubles, I had arrays of numbers and strings that I wanted to assign to variables. A slight modification of Jason&#039;s code is below which allows one to read  in variables from file assign the variables in the matlab workspace.

my input looks something like this:

mtraj{1}=&#039;src/ww1.assn.traj&#039;;
mtraj{2}=&#039;src/ww2.assn.traj&#039;;
mtraj_to_use=[1, 2];
lag_i=[ 15 ];
lag=[1, 2, 4, 8, 10, 16, 25, 50, 100, 150, 200, 250 ,300, 400, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 8000, 10000];
low_mem_map_matrix=0;
occ_threshold=0;
symm=1;
macrostate_defs=[2, 4, 6, 8, 10, 20, 40, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800];

it requires a semicolon separating all variable definitions as well as  commas between array elements. Besides those two qualifications, it follows matlab syntax. I hope this is useful to other matlab users!

&lt;pre&gt;
fid = fopen(input_file);                                                       
inputs = fscanf(fid,&#039;%s&#039;);                                                      
fclose(fid);                                              
                                                                             
limits = strfind(inputs,&#039;;&#039;);                                                   
ndx = 1;                                                                        
for(i = 1:length(limits))                                                       
  t = inputs(ndx:limits(i));                                                    
  t = strrep( t, &#039;,&#039; , &#039; &#039;);                                                    
  eval(t);                                                                      
  ndx = limits(i) + 1;                                                          
end                                                                           
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi, </p>
<p>adding onto the discussion, I wanted to do the same thing as Jason, but instead of just doubles, I had arrays of numbers and strings that I wanted to assign to variables. A slight modification of Jason&#8217;s code is below which allows one to read  in variables from file assign the variables in the matlab workspace.</p>
<p>my input looks something like this:</p>
<p>mtraj{1}=&#8217;src/ww1.assn.traj&#8217;;<br />
mtraj{2}=&#8217;src/ww2.assn.traj&#8217;;<br />
mtraj_to_use=[1, 2];<br />
lag_i=[ 15 ];<br />
lag=[1, 2, 4, 8, 10, 16, 25, 50, 100, 150, 200, 250 ,300, 400, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 8000, 10000];<br />
low_mem_map_matrix=0;<br />
occ_threshold=0;<br />
symm=1;<br />
macrostate_defs=[2, 4, 6, 8, 10, 20, 40, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800];</p>
<p>it requires a semicolon separating all variable definitions as well as  commas between array elements. Besides those two qualifications, it follows matlab syntax. I hope this is useful to other matlab users!</p>
<pre>
fid = fopen(input_file);
inputs = fscanf(fid,'%s');
fclose(fid);                                              

limits = strfind(inputs,';');
ndx = 1;
for(i = 1:length(limits))
  t = inputs(ndx:limits(i));
  t = strrep( t, ',' , ' ');
  eval(t);
  ndx = limits(i) + 1;
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2688</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Wed, 29 Jun 2011 18:25:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2688</guid>
		<description>@Ramin,

Please make sure you are constructing the name of the file correctly.  For testing, just write out the file name.  does it import then?

Doug</description>
		<content:encoded><![CDATA[<p>@Ramin,</p>
<p>Please make sure you are constructing the name of the file correctly.  For testing, just write out the file name.  does it import then?</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramin</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2678</link>
		<dc:creator>Ramin</dc:creator>
		<pubDate>Thu, 23 Jun 2011 13:39:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2678</guid>
		<description>I have same problem but my files are in “.CSV” format (Microsoft Office Excel Comma Separated Values File) and I use “csvread“to load one of my files, so I used:
&lt;pre&gt;
fileName= [ &#039;data&#039; num2str(i)];
datastruct.(fileName)=csvread([fileName&#039;.csv&#039;],0,0,[0,0,32766,0]);
&lt;/pre&gt;
But didn’t work.
I have also some “.txt” files that have some header line and I use “importdata“to load one of my files, so I used:
&lt;pre&gt;
fileName= [ &#039;data&#039; num2str(i)];
datastruct.(fileName)=importdata ([fileName &#039;.txt&#039;], &#039;,&#039;, 18);
&lt;/pre&gt;
But this didn’t work, too.</description>
		<content:encoded><![CDATA[<p>I have same problem but my files are in “.CSV” format (Microsoft Office Excel Comma Separated Values File) and I use “csvread“to load one of my files, so I used:</p>
<pre>
fileName= [ 'data' num2str(i)];
datastruct.(fileName)=csvread([fileName'.csv'],0,0,[0,0,32766,0]);
</pre>
<p>But didn’t work.<br />
I have also some “.txt” files that have some header line and I use “importdata“to load one of my files, so I used:</p>
<pre>
fileName= [ 'data' num2str(i)];
datastruct.(fileName)=importdata ([fileName '.txt'], ',', 18);
</pre>
<p>But this didn’t work, too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2587</link>
		<dc:creator>Aaron</dc:creator>
		<pubDate>Thu, 31 Mar 2011 19:20:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2587</guid>
		<description>Thanks!!

spent hours trying to figure this one out and it turns out to be beautifully simple.

One very grateful newbie</description>
		<content:encoded><![CDATA[<p>Thanks!!</p>
<p>spent hours trying to figure this one out and it turns out to be beautifully simple.</p>
<p>One very grateful newbie</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2558</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Wed, 09 Feb 2011 14:53:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2558</guid>
		<description>@Mesut,

The Dir command should help.

Doug</description>
		<content:encoded><![CDATA[<p>@Mesut,</p>
<p>The Dir command should help.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mesut</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2550</link>
		<dc:creator>Mesut</dc:creator>
		<pubDate>Mon, 07 Feb 2011 16:45:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-2550</guid>
		<description>Hi
quick question
can I pipe the names of files in a directory in to a variable array
meaning if I have bunch of text files *.asf
how can I (if possible) assign results of ls *.asf
into a variable that I can use (like a vector member by member)
thanks</description>
		<content:encoded><![CDATA[<p>Hi<br />
quick question<br />
can I pipe the names of files in a directory in to a variable array<br />
meaning if I have bunch of text files *.asf<br />
how can I (if possible) assign results of ls *.asf<br />
into a variable that I can use (like a vector member by member)<br />
thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlos Adrian Vargas Aguilera</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1504</link>
		<dc:creator>Carlos Adrian Vargas Aguilera</dc:creator>
		<pubDate>Thu, 09 Jul 2009 21:50:49 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1504</guid>
		<description>Nice post Doug.

I used to work with my ascii files as you explain. I even write a code called INT2STRZ 
http://www.mathworks.com/matlabcentral/fileexchange/12973
to work around with files named data001.txt, data002.txt, etc.

Now I prefer to use another way. First I search for the files names and create a list with the LS command (in windows) which allows me to use the wildcard &#039;*&#039; and then I just pick the file I need. So, your code will look like this:


&lt;pre&gt;
list = ls(&#039;data*.txt&#039;); % Files names on rows (WIN OS)
for k = 1:size(list,1)
 fileName = deblank(list(k,:)); % Name of k-file
 dataStruc.(fileName) = load(fileName);
end
&lt;/pre&gt;

Of course, there is an extra line, and one should be aware that all &#039;data*.txt&#039; files will be read! Other safer way is to save the filenames on a file and read instead of use LS. Well, just a thought

Carlos

</description>
		<content:encoded><![CDATA[<p>Nice post Doug.</p>
<p>I used to work with my ascii files as you explain. I even write a code called INT2STRZ<br />
<a href="http://www.mathworks.com/matlabcentral/fileexchange/12973" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/12973</a><br />
to work around with files named data001.txt, data002.txt, etc.</p>
<p>Now I prefer to use another way. First I search for the files names and create a list with the LS command (in windows) which allows me to use the wildcard &#8216;*&#8217; and then I just pick the file I need. So, your code will look like this:</p>
<pre>
list = ls('data*.txt'); % Files names on rows (WIN OS)
for k = 1:size(list,1)
 fileName = deblank(list(k,:)); % Name of k-file
 dataStruc.(fileName) = load(fileName);
end
</pre>
<p>Of course, there is an extra line, and one should be aware that all &#8216;data*.txt&#8217; files will be read! Other safer way is to save the filenames on a file and read instead of use LS. Well, just a thought</p>
<p>Carlos</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1500</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Mon, 06 Jul 2009 20:04:39 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1500</guid>
		<description>Thanks for the follow up.  Hopefully at least one other MATLAB user will find this useful!

-Doug</description>
		<content:encoded><![CDATA[<p>Thanks for the follow up.  Hopefully at least one other MATLAB user will find this useful!</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jason</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1498</link>
		<dc:creator>jason</dc:creator>
		<pubDate>Mon, 06 Jul 2009 19:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1498</guid>
		<description>So, thanks to Doug&#039;s help, it turns out that the EVAL in was not in fact the real problem.  Rather, it was that the associated m-file was not compiled with the main function.  In this case, I have hundreds of these associated m-files based on site × date combinations and didn&#039;t want to have to recompile each time I had a new dataset with a new site × date combination.  The solution is then to not use the script-based m-file (that required being compiled) and instead use a csv file.

See here for the details on compiling with associted scripts:

http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f13-1003481.html#f13-1003495

A basic example of the old script-based m-file is:
&lt;pre&gt;
Latitude = 49.3824;
Longitude = -80.4862;
Altitude = 300;
&lt;/pre&gt;

As a csv file, this is:
Latitude,49.3824
Longitude,-80.4862
Altitude,300

To make those variables available and to replace the &lt;pre&gt;eval([filename &#039;_model_parameters&#039;]);&lt;/pre&gt; command, I have this:

&lt;pre&gt;
% Get data from csv files
fid = fopen(filename &#039;_model_parameters.csv&#039;);
inputs = textscan(fid,&#039;%s%f&#039;,&#039;delimiter&#039;,&#039;,&#039;);
fclose(fid);

% Separate variables from values
vars = genvarname(inputs{1,1});
vals = inputs{1,2};

% Assign values to variables
for i = 1:size(vars,1)
        eval([cell2mat(vars(i)) &#039;= vals(i);&#039;]);
end
&lt;/pre&gt;

Thanks (again) to Doug,
Jason.</description>
		<content:encoded><![CDATA[<p>So, thanks to Doug&#8217;s help, it turns out that the EVAL in was not in fact the real problem.  Rather, it was that the associated m-file was not compiled with the main function.  In this case, I have hundreds of these associated m-files based on site × date combinations and didn&#8217;t want to have to recompile each time I had a new dataset with a new site × date combination.  The solution is then to not use the script-based m-file (that required being compiled) and instead use a csv file.</p>
<p>See here for the details on compiling with associted scripts:</p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f13-1003481.html#f13-1003495" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f13-1003481.html#f13-1003495</a></p>
<p>A basic example of the old script-based m-file is:</p>
<pre>
Latitude = 49.3824;
Longitude = -80.4862;
Altitude = 300;
</pre>
<p>As a csv file, this is:<br />
Latitude,49.3824<br />
Longitude,-80.4862<br />
Altitude,300</p>
<p>To make those variables available and to replace the
<pre>eval([filename '_model_parameters']);</pre>
</p><p> command, I have this:</p>
<pre>
% Get data from csv files
fid = fopen(filename '_model_parameters.csv');
inputs = textscan(fid,'%s%f','delimiter',',');
fclose(fid);

% Separate variables from values
vars = genvarname(inputs{1,1});
vals = inputs{1,2};

% Assign values to variables
for i = 1:size(vars,1)
        eval([cell2mat(vars(i)) '= vals(i);']);
end
</pre>
<p>Thanks (again) to Doug,<br />
Jason.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jason</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1490</link>
		<dc:creator>jason</dc:creator>
		<pubDate>Thu, 02 Jul 2009 19:31:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1490</guid>
		<description>I was just thinking about this last week.  Is there a comparable solution, using load instead of eval, that would also keep the Matlab compiler happy with scripts (.m files)?

For example, if I had a series of parameters as a script (a .m file), this won&#039;t work with the compiler:

eval([filename &#039;_model_parameters&#039;]);

Is there an equivalent with load that would work?

Thanks for the videos.
Jason.</description>
		<content:encoded><![CDATA[<p>I was just thinking about this last week.  Is there a comparable solution, using load instead of eval, that would also keep the Matlab compiler happy with scripts (.m files)?</p>
<p>For example, if I had a series of parameters as a script (a .m file), this won&#8217;t work with the compiler:</p>
<p>eval([filename '_model_parameters']);</p>
<p>Is there an equivalent with load that would work?</p>
<p>Thanks for the videos.<br />
Jason.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

