Using eval versus other methods

9 次查看(过去 30 天)
Dan
Dan 2011-4-19
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

采纳的回答

Jan
Jan 2011-4-20
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 个评论
Dan
Dan 2011-4-20
"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".
Dan
Dan 2011-4-20
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

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2011-4-19

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by