1

I have different size of arrays such as [6:8], [11:21], [14:15], etc. I need to assign these values to a variable set which should contain strings specified in a different array. Let me give an example:

variables = ["a", "b", "c"];

% I need to design a structure just like below but inside a for loop. 
% Because I've lots of variables and arrays.
xx.a = [6:8];
xx.b = [11:21];
xx.c = [14:15];

Is there any solution proposal?

0

1 Answer 1

1

I don't know how your data is organized, but you can use the strings of your variables array to index the struct fields dynamically:

variables = ["a", "b", "c"];
data{1} = [6:8];
data{2} = [11:21];
data{3} = [14:15];

% Use string indexing of structs within loop
for ii = 1:numel(variables)
    xx.(variables(ii)) = data{ii};
end

xx = 

  struct with fields:

    a: [6 7 8]
    b: [11 12 13 14 15 16 17 18 19 20 21]
    c: [14 15]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. That's what I'm looking for. Thanks a lot

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.