From: Corinne on 11 Jun 2010 15:42 Hello, I would like to know how to evaluate a character as a variable when the character is the name of a variable. I ask because I would like to set up a for loop that cycles over a number of variables. I have the names of the variables I want to cycle through in a variable called 'varNames'. Below is a dummy code to give an example of what I would like to do. The point of the code is to extract the maximum of the data in every variable and field. The problems that I would like help with are commented. Thanks in advance for the help!! Corinne var1.data1=[3,4,2]; var1.data2=[3,7,5]; var1.data3=[3,5,2,1,4,5]; var2. data1=[3,2,3,2,1]; var3.data1=[3,4]; var3.data2=[3,6,5,4]; var3.data3=[2,3,3,3,3,1,1,1]; var3.data4=2; var3.data5=[3,2,1]; varNames={'var1', 'var2', 'var3'} for i=1:length(varNames) fieldNames=fields(varNames{i}) %varNames{i} is a character as opposed to a variable even though it is the name of a variable. How do I get this to evaluate as a variable so that the fields in that variable are returned in fieldNames? for j=1:length(fieldNames) dataMax(i,j)=max((varNames{i}).(fieldNames{j})); %same problem here, how do I get (varNames{i}) to evaluate as a variable just like .(fieldNames{j}) will evaluate as a field. end end
From: Walter Roberson on 11 Jun 2010 15:51 Corinne wrote: > Hello, I would like to know how to evaluate a character as a variable > when the character is the name of a variable. I ask because I would > like to set up a for loop that cycles over a number of variables. I > have the names of the variables I want to cycle through in a variable > called 'varNames'. That is very much recommended against; it will almost certainly lead you to hard to find bugs. You would, for example, have to verify that none of the variable names happened to match the name of any function called in the same routine. > for i=1:length(varNames) > fieldNames=fields(varNames{i}) %varNames{i} is a character as > opposed to a variable even though it is the name of a variable. How do > I get this to evaluate as a variable so that the fields in that variable > are returned in fieldNames? Don't do that if you can avoid it. Push everything down one level into a single structure, e.g., fieldNames = fields(MyVars.(varnames{i})); http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
From: Matt Fig on 11 Jun 2010 16:16 You could do this using EVAL, but I recommend instead that you change your data storage scheme. Among the many alternatives ways to doing what you are trying to do is: var(1).data{1}=[3,4,2]; var(1).data{2}=[3,7,5]; var(1).data{3}=[3,5,2,1,4,5]; var(2). data{1}=[3,2,3,2,1]; var(3).data{1}=[3,4]; var(3).data{2}=[3,6,5,4]; var(3).data{3}=[2,3,3,3,3,1,1,1]; var(3).data{4}=2; var(3).data{5}=[3,2,1]; for ii = 1:length(var) for jj = 1:length(var(ii).data) dataMax2(ii,jj) = max(var(ii).data{jj}) ; end end Now you could also use structfun to get datMax2 instead of looping like this, or even cellfun.
|
Pages: 1 Prev: Passing variables between GUI calback functions Next: open file match string with white-space |