0

I have a question regarding this type of code:

First = ["A","B","C"];
Second = ["D","E","F"];
Group = ["First", "Second"];
for gr = Group
    current = gr;        
    for number = 1:numel(current)                
        my_variable(number) = current(number);     
    end
end

That's the reproduction of my problem. In this case my_variable is equal to "First" for example. But I wanted it to be "A" then "B" then "C" from variable named "First".

For my understanding this code should do the following:

  • 1st step: for gr = Group means gr = First to Second
  • 2nd step: current = gr; means current = First
  • 3rd step: for number = 1:numel(current) means number = 1:3 (number of elements in "First")
  • 4th step: my_variable(number) = current(number); means my_variable = First(1) = "A"

Instead of that I get my variable equal to "First" or "Second".

I hope you understand what I mean.


P.S. the string arrays I'm using with double quotes (" ") like First = ["A","B","C"]; are only available in Matlab 2016b or later.

7
  • You have misinterpreted step 3. numel(current) = numel('First') = 5 as in there are 5 elements in the string First. You could do numel(eval(current)) which would evaluate the variable from the string, but please don't - using eval is bad practice, especially when easily avoidable like here. Instead try using structs or cell arrays Commented Oct 3, 2017 at 12:02
  • Thank you for your comment! Now I see where my understanding is different from what Matlab wants to do. Unfortunely, changing those variables to cell arrays/structure arrays, doesn't change the bad output (still get the name of the variable "First" or "Second" instead of corresponding value, for example "A" or "B". I will try this method with eval instead :(. Commented Oct 3, 2017 at 13:15
  • Also, method with "eval" certainly works for counting (step 3), but that's not the solution to my real problem described above. Do you have any other ideas? Thanks! Commented Oct 3, 2017 at 13:18
  • What do you actually expect the output to look like here? Because even if this did what you expected, you would over-write my_variable with the values in Second, and those extracted from First are lost... Commented Oct 3, 2017 at 13:44
  • Yes, you're right, but I can repair it later. The real thing is that I want to store two variables in another one, and I want to use that other one in a loop. What I need for my solution is the loop where firstly it takes every element of the first variable (i.e. from variable First, it takes "A", "B" then "C"; and then in a loop it takes every element from variable Second - "D", "E", "F"). I made an error with creating this variable "Group" for loop, because in code it's always a string or structure or cell, but it never creates the "link" to real existing variable "First" or "Second". Commented Oct 3, 2017 at 13:52

1 Answer 1

2

You should use cell arrays to do this:

first = {'A','B','C'};
second = {'D','E','F'};
group = {first, second};
for group_ind = 1:numel(group)
    current = group{group_ind};
    my_variable = cell(1,numel(current));
    for number = 1:numel(current)
        my_variable{number} = current{number};
        disp(my_variable)
    end
end

For first and second, you can also use the string arrays:

first = ["A","B","C"];
second = ["D","E","F"];
group = {first, second};
for group_ind = 1:numel(group)
    current = group{group_ind};
    my_variable = strings(1,numel(current));
    for number = 1:numel(current)
        my_variable(number) = current(number);
        disp(my_variable)
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.