Naming variables with other variables

  • Thread starter Thread starter Odysseo
  • Start date Start date
  • Tags Tags
    Variables
AI Thread Summary
The discussion revolves around a user seeking help with a MATLAB code snippet intended to create datasets from multiple sources using nested loops. The user is trying to dynamically name new datasets based on source names and group identifiers, but encounters issues with the implementation, particularly with variable naming and dataset retrieval. Responses clarify that the code is indeed written in MATLAB, and suggestions are made to avoid using `eval`, which can complicate the code. Instead, alternatives such as constructing strings for filenames and checking for their existence are recommended. Ultimately, the user decides to rewrite the code in Python, which resolves their issues and allows the code to function as intended.
Odysseo
Messages
3
Reaction score
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
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
 
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).
 
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
 
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
 
Thank you all. Finally I've preferred to re-write the code in Python. So now works!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top