Using eval versus other methods

Hello,
I am currently working on a script to automate some number crunching and am wondering if there is a better (more efficient) way to write the code without having to use eval.
Our main problem is using the dates in the file names as variables and being able to index correctly while performing various functions.
Thank you for your help,
Dan the Man
%load in all 1 minute averaged files and perform calculations and generate
%plots
%--------------------------------------------------------------
%define days of month in text file names, MUST BE CHANGED EVERYTIME YOU
%LOAD IN A NEW GROUP OF FILES!
day = [9, 10];
%import all text files from one directory at once
AVGfiles = dir('*AVG.txt');
for i=1:length(AVGfiles)
eval(['load ' AVGfiles(i).name ' -ascii AVG']);
end
%------------------------------------------------------------------------
for i = 1:length(AVGfiles)
j = day(i);
%define length of each file to be used in total wind calculation
eval(['k = length(Apr' num2str(j) 'AVG)'])
%compute turbulence for each minute for each text file
eval(['TKE_' num2str(j) ' = 0.5 * (Apr' num2str(j) 'AVG (:,19) + Apr' num2str(j) 'AVG (:,20) + Apr' num2str(j) 'AVG (:,21))'])
%compute total wind
for a = 1:k
eval(['total_wind_' num2str(j) '(a) = ((Apr' num2str(j) 'AVG (a,7))^2 + (Apr' num2str(j) 'AVG (a,8))^2 + (Apr' num2str(j) 'AVG (a,9))^2)^(1/2)'])
end
eval(['mph_wind_' num2str(j) ' = total_wind_' num2str(j) '/100'])
eval(['mph_wind_' num2str(j) ' = (mph_wind_' num2str(j) ' * 3600) / 1609.344'])
%compute datenumber for each file to use in plotting
eval(['datenumber_' num2str(j) ' = datenum(Apr' num2str(j) 'AVG (:,3), Apr' num2str(j) 'AVG (:,1), Apr' num2str(j) 'AVG (:,2), Apr' num2str(j) 'AVG (:,4), Apr' num2str(j) 'AVG (:,5), Apr' num2str(j) 'AVG (:,6))'])
%generate a TKE plot for each day
eval(['plot(datenumber_' num2str(j) ', TKE_' num2str(j) ')'])
datetick('x', 'mm-dd-yyyy')
ylabel('TKE')
title(['UNR Roof Apr ',num2str(j),' TKE'],'Fontsize',14)
saveas(gcf,['Apr_',num2str(j), 'TKE'], 'png')
%generate a total wind plot for each day
eval(['plot(datenumber_' num2str(j) ', mph_wind_' num2str(j) ')'])
datetick('x', 'mm-dd-yyyy')
ylabel('Wind Speed (mph)')
title(['UNR Roof Apr ',num2str(j),' Total Wind Speed'],'Fontsize',14)
saveas(gcf,['Apr_',num2str(j), 'wind'], 'png')
end

 Accepted Answer

Convert:
eval(['load ' AVGfiles(i).name ' -ascii AVG']);
to:
Data = load(AVGfiles(i).name, '-ascii', 'AVG');
Then "Data.(['Apr', num2str(j), 'AVG'])" is much safer and more efficient than the EVAL constructions.

4 Comments

Thanks Jan, but I don't think I fully understand your answer. I converted the first eval statement like you said and then I tried to create a variable as follows:
for i = 1:length(files)
j = day(i);
%define length of each file to be used in total wind calculation
%eval(['k = length(Apr' num2str(j) 'AVG)'])
k = length( Data.(['Apr', num2str(j), 'AVG']));
end
And I get the following error: "Attempt to reference field of non-structure array."
Anymore help would be greatly appreciated!
Thanks,
Dan
What is the contents of the variable "Data"?
The idea is to use "dynamic fieldnames" and indices instead of creating variables which include an index in the name. "Name1" is tedious, "Name{1}" is easy.
"Data" is a 1440x24 matrix with each column being a different variable output from an anemometer (i.e. wind speed, wind direction, etc.). Each file I load in is one day's worth of data and I need to perform computations for each day using several different columns from "Data".
Jan,
I figured out a way to solve the problem using cell array with the help of you last comment. It's soooo much faster! Thanks again!!
Dan

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

Dan
on 19 Apr 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!