Renaming of Structure field inside a Structure with data inside the field
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
David Sanchez
am 23 Jul. 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
am 23 Jul. 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 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!