Working with a sturcture, rename top layer by a cell array

Dear comunity,
i'm really struggling by reaming the top layer of a stucture.
so i have a structure s
s = 1×3 struct array with fields: f1 f2 ....f12 (each field contains a datetime and a double vector)
s(1:3).field(1:12).time
s(1:3).field(1:12).data
well i do not have a problem with renaming the field part, its easy by just using dynamic variables:
newname = {'something1', 'something2', 'something3'}; j = 1;
s.(newname{j}).data gives me s.something1.data
BUT my problem starts, when i want to rename the top layer of the sturcture:
desired output would be: something1.field.time/ something1.field.data
newname{j} = s(1)
newname = 1×3 cell array with {1×1 struct} {'something2'} {'something3'}
well herby the sturcture changes just to newname{1, 1}
any hints to do better?
regards, MLP

9 Comments

And this applies to field names as well. You shouldn't have fields f1, f2, ... you should have one indexed field f (so f(1), f(2), ...) which would completely remove the need for dynamic field names.
Thanks guys for your fast responds, but probably you misunderstood me. I do not use/ want indexing within my variable names. f1, f2 were just a placeholder for a character vector like 'mz' 'int' 'wl'. They are total independed from each other without any indexing. So with 'something1' or 'something2' I just wanted to decribe independed charakters like 'AWH', 'VTB' 'GOE'
therefor i'm still searching for a solution to get: AWH.field.data based on s(1) and newname={'AWH', 'VTB', 'GOE'}
What is almost nearby is assignin('base', newname{1}, s(1))
I don't think it's possible to do what you're asking, from a literal renaming perspective. The top layer of a structure is the variable itself, and I don't know of anyway within Matlab to rename variables.
You can always copy the contents of the variable into a different variable, but you wouldn't likely be able to do so dynamically because I have yet to see a way to dynamically generate variables. You could turn 'newname' into a structure and index through the different names that way though if you would like. Or do cells.
Well, there is a way to dynamically generate variables, but as per Walter's link, it's universally a bad idea. You're not only writing code to manipulate the content of your variables, you're now also writing code to manipulate the name of the variables. Double the work for no gain (because there's always a much simpler way).
@MeLearningProgramming, I think it would be a good idea if you explained the context of your request. Why do you want to do what you're asking? I suspect that using a completely different way of storing your data would make it much easier to work with.
Wow, there is a way to dynamically generate names? I feel like I've just discovered the underworld of Matlab.
Follow Walter's link and do exactly what it says not do to access that underworld. A world full of eval, assignin, evalin and co. Matlab hell!
hihihi ... thanks guys.
s ist the varagout of my import funktion. Now I wanted to prepare parts of the sturcture s as an input and other parts of the structure s as output for the fitnet/narxnet.
InputVektor = [AWH.field.data, GOE.field.data];
TargetVektor = [VTB.field.data];
regards
The question is why do you want to make structure 's' a 3x1 struck array in the first place if what you really want is three independant structures.
You mentioned s is the ouput of an import function. If this function is generic and you want to break the data out differently after its been imported, simply assign the data in s to new variables.
AWH = s(1);
VTB = s(2);
GOE = s(3);

Sign in to comment.

Answers (0)

Categories

Edited:

on 5 Aug 2019

Community Treasure Hunt

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

Start Hunting!