Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Naming variables with other variables

  1. Aug 3, 2016 #1
    Hallo, this is my first thread here. I have to do a double loop.
    I've a 1x4 cell containing 4 sources. For each of this sources i have a number of dataset labeled as source_pN where N is a number (a label for a group). I have to do a double "for" loop on sources and on each existing N to create another dataset with histc on previous dataset variables. So i would like to name those new datasets using both source name and N number. I've done something like this:

    Code (Matlab M):

    sources = {'BKG' 'Ba133' 'Co57' 'Cs137'};

    for is = 1:length(sources)

        sourcename = sources{is};
        source = eval(sprintf('%spixels', sourcename));
        source.groups = source.nx*100+source.ny;

        for grp = 1:length(unique(source.groups))

            pix = source.groups(grp);

            if -SOMETHING TO CHECK WHICH N-
                data = eval(sprintf('%s_P%i',sourcename,pix));
                Qbins.data = 0:0.05:max(data.Qnet);
                Qpop.data = histc(data.Qnet, Qbins.data);
            else
                continue
            end

        end
    end
     
    But it doesn't work. How can i solve my problem? Any hints?
     
    Last edited by a moderator: Aug 3, 2016
  2. jcsd
  3. Aug 3, 2016 #2

    jedishrfu

    Staff: Mentor

    What language are you writing in?

    I assumed it was MATLAB and added code tags and some blank lines to your post for readability of the code.

    If this is in fact C or Java code then the for loops need to have braces to enclose the loop body otherwise the for will loop on the first statement only.
    Code (Java):

    for(int i=0; i<10; i++)
        println("Hello");       // semi-colon denotes end of this loop
        println("goodbye ... executed once only as loop ended with previous ;...");

    //         vs

    for(int i=0; i<5; i++) {
        println("hello");
        println("hello again");
    }    // brace denotes end of this loop
     
     
  4. Aug 3, 2016 #3

    Hi, thank you, my code is written in Matlab. Perhaps it's not properly correct, but i'm rather sure that the "for" loop is right (it stops with the "end" statement). However, my problem is to having variables named by another variables and to search for a dataset among the others (the if statement).
     
  5. Aug 3, 2016 #4

    jedishrfu

    Staff: Mentor

    It looks like you are trying to build a filename from a string and a number.

    Here's some examples from the MATLAB site:

    https://www.mathworks.com/matlabcentral/newsreader/view_thread/290652

    Notice they use sprintf to create a formatted string and don't need the eval.

    A common programming task is to construct a filename as you have done and then check to see if the file exists by opening it or by some file exists function.

    https://www.mathworks.com/matlabcentral/answers/49414-check-if-a-file-exists
     
  6. Aug 3, 2016 #5

    jack action

    User Avatar
    Science Advisor
    Gold Member

    Have you tried this (source):

    Code (Text):
    sources = {'BKG' 'Ba133' 'Co57' 'Cs137'};

    for is = 1:length(sources)

        sourcename = sources{is};
        eval([source  ' = ' sprintf('%spixels', sourcename) ';']); % Change here
        source.groups = source.nx*100+source.ny;

        for grp = 1:length(unique(source.groups))

            pix = source.groups(grp);

            if -SOMETHING TO CHECK WHICH N-
                eval([data ' = ' sprintf('%s_P%i',sourcename,pix) ';']); % Change here
                Qbins.data = 0:0.05:max(data.Qnet);
                Qpop.data = histc(data.Qnet, Qbins.data);
            else
                continue
            end

        end
    end
     
  7. Aug 10, 2016 #6
    Thank you all. Finally i've preferred to re-write the code in Python. So now works!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Naming variables with other variables
  1. Global variables. (Replies: 7)

  2. Valid variable names (Replies: 4)

Loading...