how to uncombine variables from one variable?

4 vues (au cours des 30 derniers jours)
cemsi888
cemsi888 le 23 Jan 2015
Commenté : Image Analyst le 23 Jan 2015
Hi again i have one variable which contains 181 variables. do you know how to uncombine these variables from one variable?
  2 commentaires
Geoff Hayes
Geoff Hayes le 23 Jan 2015
Pleas clarify - do you have a struct variable with 181 fields, or a cell array of variables, or something else? Provide a small example of what you have and what you would like to see after the variable has been broken apart.
cemsi888
cemsi888 le 23 Jan 2015
Modifié(e) : Image Analyst le 23 Jan 2015
I uploaded a picture. I have 181 fields in one structure variable.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 23 Jan 2015
I have no idea why you want to do this - it seems like it would make the program less convenient after this. Of course you could always use the brute force way:
U_in_1 = s.U_in_1;
dp_SL_amp = s.dp_SL_am;
% and so on for the remaining 179 items...
You could tuck all that inside a function rather than clutter up your main program.
You could also use a loop if you call fieldnames() and eval().
  2 commentaires
cemsi888
cemsi888 le 23 Jan 2015
thanx a lot!! yes i can call fieldnames but i could not make out Loop. could you give me some advices?
Image Analyst
Image Analyst le 23 Jan 2015
Try this:
% Reference: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
% Create sample data.
myStruct.field1 = 10;
myStruct.field2 = [20, 30, 40];
myStruct.field3 = 'abc';
myStruct.field4 = {'a cell array', 'cell #2'};
% Get the field names of the structure.
fn = fieldnames(myStruct)
% Go through all field names creating a variable with
% the name of that field, and with a value the same as that field.
for k = 1 : length(fn)
commandString = sprintf('%s = myStruct.%s', fn{k}, fn{k});
eval(commandString);
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by