Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

Matlab eval having trouble with simple matlab scripts

So i have a script which should run a series of other scripts, gather data from them and use that data.

My file structure looks like this:

Results
    -result001.m
    -result002.m
    ...
    -result100.m
    -DataFromICP.m

Now dataFromICP at this point should simply loop through all the results and concatenate them to a struct called pointsAndTimeS:

resultsFiles = dir('result*');
pointsAndTimeS = struct('points', zeros(length(resultsFiles)), 'times', zeros(length(resultsFiles)));
resultsFiles
count = 1;
for i = 1:length(resultsFiles)
     resultsFiles(i).name
     eval(resultsFiles(i).name)
     pointsAndTimesS.points(i) = numberOfPointsRead;
     pointsAndTimesS.times(i) = PoseEstimates(length(PoseEstimates)).timeElapsed;

end

Now it correctly iterates through the files, that is:

resultsFiles = 

3x1 struct array with fields:
name
date
bytes
isdir
datenum

where the names are result001.m through to result100.m

But i get the error from eval saying:

Undefined variable "result10" or class "result10.m".

Error in DataFromICP (line 7)
   eval(resultsFiles(i).name)

Does anyone know what's going on?

Upvotes: 0

Views: 238

Answers (1)

ackrause
ackrause

Reputation: 442

eval expects a string as input, so it's trying to evaluate the input you give it (which isn't a string) as a variable or the name of a script. To give it the value in resultsFiles(i).name, this should work:

eval(sprintf('%s', resultsFiles(i).name));

Upvotes: 1

Related Questions