<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Advanced: loading files using their names for variable names</title>
	<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>
	<pubDate>Sun, 22 Nov 2009 23:28:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Ashok kumar</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1571</link>
		<dc:creator>Ashok kumar</dc:creator>
		<pubDate>Fri, 11 Sep 2009 12:40:35 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1571</guid>
		<description>Thanks for the video tutorial. It made my life easy.</description>
		<content:encoded><![CDATA[<p>Thanks for the video tutorial. It made my life easy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jhuang</title>
		<link>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1507</link>
		<dc:creator>jhuang</dc:creator>
		<pubDate>Fri, 17 Jul 2009 08:13:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/#comment-1507</guid>
		<description>exactly what i needed :)

thanks doug</description>
		<content:encoded><![CDATA[<p>exactly what i needed :)</p>
<p>thanks doug</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>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 '*' and then I just pick the file I need. So, your code will look like this:


&lt;pre&gt;
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
&lt;/pre&gt;

Of course, there is an extra line, and one should be aware that all 'data*.txt' 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>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>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'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'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 '_model_parameters']);&lt;/pre&gt; command, I have this:

&lt;pre&gt;
% 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
&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>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't work with the compiler:

eval([filename '_model_parameters']);

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 &#8216;_model_parameters&#8217;]);</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>
