Dynamic Objects in Matlab

2019-08-15 02:37发布

问题:

I have a scenario to create dynamic objects and also their variables by just entering the number of objects in for loop and the desired output is to initialize the variable name and assignment to corresponding array of position vector, in Matlab. However I tried eval() but it doesn't help me as i want to write a function and other complexities, I teried very much but could find the proper solution of my scenario. What suggestions should according to my scenario, may be with function or without. Edit1: Position vector is predefined. String for number the object, its not compulsory. My Code:

function myFunc(n)
for ii=1:n
Obj(ii) = 'some string required with its number of ii';
 Obj(ii).position=position(ii);
end
end

回答1:

Have you tried assigning your objects to a cell structure?

You can define a cell structure with two position indices, and the cell structure can hold other objects, e.g. fit objects, linear models

models = cell(100,100) 
for i = 1:100
    for j = 1:100
        Obj{i,j} = ....  % where ... is your expression that generates an object
    end
end

From inside the cell structure, you can open the object and inspect all the different types of variables, e.g. tables, doubles, formulas, cells, str, chararray etc.