1

Suppose the next variable:

B=genvarname(strcat('B',num2str(25),''));

And B will be:

B=B25;

Now, I want covert "B25" in a struct object, so I did this:

 eval([B ' = struct([])']);

With that code I convert B25 (who is saved in "B") into a struct object.

Now, I want to save another struct object into B25, like this one:

A = 

    a1: 1
    a2: 2
    a3: 'a'

As you can see, A is a struct too.

Here is where I need your help, I don't know how to do something like this:

somefunction(B,A);

And stick B with A, like this:

B25.A=

        a1: 1
        a2: 2
        a3: 'a'

Thanks!

3 Answers 3

1

You can go on with nasty evals and do:

eval([B '.A = A;'])

Which will work, but is really bad style and makes things worse by the time you use more and more variable names like B25.

The proper question however is, why are you using a dynamic, "numbered" variable name instead of a struct array or cell array? If you have 25 B's, things could look as easy as:

B(25).A = A; % using a struct-array

or

B{25}.A = A; % using a cell array

That's what lists in progamming languages are meant for - to avoid the need of variable names like B1, B2, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not simply:

A = struct('a1', 1, 'a2', 2, 'a3', 'a');

and

B = struct('a', A)

2 Comments

Sorry, but that is not the answer that I need, look the question please.
Then please post a proper question. It's absolutely unclear what you want to achieve.
0
B=genvarname(strcat('B',num2str(25),''));
VaibleName='A';
Data= struct('a1', 1, 'a2', 2, 'a3', 'a');
eval([B strcat(['=struct(''',VaibleName,''', Data)'''])])

B25.A

ans = 

    a1: 1
    a2: 2
    a3: 'a'

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.