Naming variables with other variables

  • Thread starter Odysseo
  • Start date
  • Tags
    Variables
In summary, you have to do a double loop to create two datasets, one with histograms on the previous dataset variables and one with the pixel values.
  • #1
Odysseo
3
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:

Matlab:
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:
Technology news on Phys.org
  • #2
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.
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
 
  • #3
jedishrfu said:
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.
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
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).
 
  • #4
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
 
  • #5
Have you tried this (source):

Code:
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
 
  • #6
Thank you all. Finally I've preferred to re-write the code in Python. So now works!
 

1. Can you give an example of naming variables with other variables?

Yes, for example, you can declare a variable called "firstName" and assign it the value "John". Then, you can declare a second variable called "lastName" and assign it the value "Smith". Finally, you can declare a third variable called "fullName" and assign it the value of firstName + " " + lastName. This will result in the value of "John Smith" being stored in the "fullName" variable.

2. What is the benefit of naming variables with other variables?

Naming variables with other variables allows for more efficient and dynamic code. It allows you to use the values of existing variables to create new variables without hardcoding them. This can save time and effort when writing code.

3. Can you use multiple variables to name a single variable?

No, variables can only have one name. However, you can use multiple variables to create a new variable with a meaningful name, as shown in the previous example.

4. Are there any rules for naming variables with other variables?

The same rules for naming variables apply when using them to name other variables. Variable names must start with a letter, underscore, or dollar sign, and can contain letters, numbers, underscores, or dollar signs. They cannot contain spaces or special characters.

5. Is it considered good practice to name variables with other variables?

It depends on the specific situation and personal preference. In some cases, it may make the code more readable and efficient. However, overuse of this technique can make the code more complex and difficult to understand. It is important to find a balance and use this method when it makes sense and improves the code.

Back
Top