Renaming of Structure field inside a Structure with data inside the field
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am having some text files having some data , i have created a structure of those text files using the dir function , that created a structure of sturctures , now inside the inner sturcture the field=name, is having the text file name that the dir function imported , i want to use the string(data or name) without the extension (i.e. '.txt' ) inside the name field as the new name
for eg. my structure is text_files having 20 elements each structure inside the text_files structure is having 5 fields namely
name data bytes isdir datenum
created because of the dir function now the 'name' field is having the name of the text file imported by the dir function which is 'amit.txt' for eg. , i want the 'name' field to be replaced by 'amit' which is data inside the 'name' field and for each structure i want the name field to be replaced by the string data inside it , i know that in matlab workspace editor i can rename the 'name' field mentioned above by right clicking and selecting the option rename but i want code level implementation for it as i am writing a code to do this thing , please provide me with the solution i am stuck with this.
Regards, Amit
0 commentaires
Réponses (2)
David Sanchez
le 23 Juil 2013
I think this is what you are looking for:
my_struct.name = 'xxxx.txt'; % sample struct
new_field = my_struct.name(1:end-4); % delete the extension
my_struct.(new_field) = my_struct.name;
my_struct = rmfield(my_struct,'name'); % remove the old filed name
Jan
le 23 Juil 2013
In a struct array all structs have the same fieldnames. Otherwise it is not a struct array anymore. You can use a cell, if you want to store structs with different fieldnames.
Note:
myStruct.name = 'xxx'
is usually much more efficient and clearer than:
myStruct.xxx = 'abc';
0 commentaires
Voir également
Catégories
En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!