MATLAB Answers

Help with "find" (possibly inside of "eval"?)

38 views (last 30 days)
Hi everyone:
Here is my issue...
I am inputting numerous text files inside of a FOR loop in order to pull two particular columns out, one with all characters and one with numbers (double). The code that I have so far, copied below, works as desired. Note that I set "numsvns" as only one entry for debugging.
numsvns = [61]; %only one number for debugging purposes
home_path = path;
for i = 1:length(numsvns)
% Set the current SVN number as an integer
svn1 = numsvns(i);
% Change the path to the folder of the current SVN
% % Convert the integer SVN to a string
svn = int2str(svn1);
% % Append the SVN to the folder needed to open
directory = strcat('output/sv',svn);
% % Open the folder for the current SVN
% Open the file and turn the data into a cell array
fid = fopen('groundVisibilityEvents.txt');
eval(['gve' svn ' = textscan(fid,''%s %s %f %f %s %s %s %f'');'])
% Pull out the data of interest (site (col. 1) and duration (col. 8))
eval(['site' svn ' = gve' svn '{1};'])
eval(['duration' svn ' = gve' svn '{8};'])
This creates two variables of importance, one called "site61" with ASCII characters and one called "duration61" with the double times. Of course, the variables have the 61 appendix since I set "numsvns" = 61 alone.
My next step is to search through "'duration' svn" (in this case, "duration61") in order to find the indices of zero terms. The following works great, as expected:
index = find(duration61==0);
However, I cannot figure out how to generalize this inside of my FOR loop. The following commands don't work, because the "find" function interprets the "duration61" as a character string rather than array:
index = find(['duration' svn]==0);
or, eval(find(['duration' svn]==0));
or, eval(find(['duration' str2num(svn)]==0));
or, abc='duration61'; find(abc==0);
Is there a way to turn a string into a variable? I thought that "eval" should have done that, but no luck. Otherwise, how do I generalize the "find" command to work with a generic "duration##", where the ## is specified in the loop?
Thank you,
  1 Comment
Andrew Newell
Andrew Newell on 20 Jul 2011
Have a look at

Sign in to comment.

More Answers (1)

Jan on 20 Jul 2011
Do not insert the index in the name of the variable. Better use and index as index: "duration{61}" is more powerful and avoids the ugly EVAL stuff.

Community Treasure Hunt

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

Start Hunting!