Borrar filtros
Borrar filtros

Renaming of Structure field inside a Structure with data inside the field

2 visualizaciones (últimos 30 días)
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

Respuestas (2)

David Sanchez
David Sanchez el 23 de Jul. de 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
  1 comentario
Amit
Amit el 23 de Jul. de 2013
Hello David , Thanks for the reply , though with this i am able to move a little bit but still i am unable get the problem solved , as the above code which you have mentioned is for a single structure, what i have with me is structure of structures , if i am trying to rename the field of a single sub-structure inside the main structure then it is getting renamed in all other sub-structures also
for eg. my problem is like this I have a folder having some text files. now i have used the dir function , it created a structure 'text_files' having around 20 other sub-structure inside it with the fields name,data ,bytes ,isdir ,datenum , i can access the content of those sub-structure sinside the text_files structure by using text_files(2,1).name (if i want the data inside the 'name' field of second sub-structure of the text_files structure), the data inside the name field is the name of the file that the dir function imported i.e 'xxxxx.txt' , now i want to replace the field 'name' in the second sub-structure inside the text_files structure to be replaced with 'xxxxx' , but when i am using
new_filed=text_files(2,1).name(1:end-4); text_files(2,1).(new_field)=my_struct(2,1).name;
then also a new field 'xxxxx' is getting created for all other remaining 19 sub-structures inside text_files structure ,
and also for removing the field the command you have provided i.e my_struct = rmfield(my_struct,'name'); % remove the old filed name
is not working instead of that i have used
rmfield(my_struct,'name'); % remove the old filed name
only i dont know why the one you provided is generating some errors
Hope you will be able to get what i am trying to say as this is getting confusing for me itself to explain.
Regards, Amit

Iniciar sesión para comentar.


Jan
Jan el 23 de Jul. de 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';

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by