Renaming of Structure field inside a Structure with data inside the field
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (2개)
David Sanchez
2013년 7월 23일
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
2013년 7월 23일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!