Concatenate name fields in nested structure

조회 수: 1 (최근 30일)
Elzbieta
Elzbieta 2024년 6월 13일
댓글: Voss 2024년 6월 15일
Hello,
I am trying to save value of the data placed in the last fields in nested structure by concatenating string in recursion. The name of the file should string composed of the all preceding fields in structure.
However I am not able to save previous fields.
The code is as follows:
nameDataBase = 'database.mat';
structNest = load([pathFolder, nameDataBase]);
structFields = myIsField(structNest, 'data');
pathOutputFile = pathFolder;
fileName = ''
function isFieldResult = myIsField (inStruct, fieldName)
% inStruct is the name of the structure or an array of structures to search
% fieldName is the name of the field for which the function searches
B = struct();
display('output folder')
isFieldResult = 0;
f = fieldnames(inStruct(1))
f1 = string(fieldnames(inStruct(1)))
for i=1:1%length(f)
display("i")
i
if(strcmp(f{i},strtrim(fieldName)))
isFieldResult = 1;
display('data found - end')
B.(f{i}) = inStruct(1).(f{i})
data = getfield(B,'data')
return;
elseif isstruct(inStruct(1).(f{i}))
isFieldResult = myIsField(inStruct(1).(f{i}), fieldName);
B.(f{i}) = inStruct(1).(f{i})
if isFieldResult
display('time found -loop')
return;
end
end
end
  댓글 수: 1
Ganesh
Ganesh 2024년 6월 13일
If you could upload the file 'database.mat' and describe what the output you would like to see is, it would be very helpful.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 6월 13일
S = struct( ...
'A1',1, ...
'A2',struct('B1',2,'not_data',3,'data',4), ...
'A3',struct( ...
'B1',struct('not_data',5), ...
'B2',struct( ...
'C1',struct('data',6), ...
'data',struct( ...
'D1',struct('data',7)))))
S = struct with fields:
A1: 1 A2: [1x1 struct] A3: [1x1 struct]
S.A2
ans = struct with fields:
B1: 2 not_data: 3 data: 4
S.A3
ans = struct with fields:
B1: [1x1 struct] B2: [1x1 struct]
S.A3.B1
ans = struct with fields:
not_data: 5
S.A3.B2
ans = struct with fields:
C1: [1x1 struct] data: [1x1 struct]
S.A3.B2.C1
ans = struct with fields:
data: 6
S.A3.B2.data
ans = struct with fields:
D1: [1x1 struct]
S.A3.B2.data.D1
ans = struct with fields:
data: 7
function [out,data] = get_full_field_names(S,name,str)
out = strings(0);
data = cell(0);
if nargin < 3
str = "";
end
f = fieldnames(S);
for ii = 1:numel(f)
if isstruct(S.(f{ii}))
[new_out,new_data] = get_full_field_names(S.(f{ii}),name,str+f{ii}+".");
out = [out, new_out];
data = [data, new_data];
elseif strcmp(f{ii},name)
out = [out, str+name];
data = [data, {S.(f{ii})}];
end
end
end
[names,data] = get_full_field_names(S,'data')
names = 1x3 string array
"A2.data" "A3.B2.C1.data" "A3.B2.data.D1.data"
data = 1x3 cell array
{[4]} {[6]} {[7]}
[names,data] = get_full_field_names(S,'not_data')
names = 1x2 string array
"A2.not_data" "A3.B1.not_data"
data = 1x2 cell array
{[3]} {[5]}
[names,data] = get_full_field_names(S,'something_else')
names = 0x0 empty string array data = 0x0 empty cell array
  댓글 수: 5
Elzbieta
Elzbieta 2024년 6월 15일
이동: Stephen23 2024년 6월 15일
Thank you for your comments. Voss your solution works great!
Voss
Voss 2024년 6월 15일
You're welcome and thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by