Find the existence of a character from a struct
11 次查看(过去 30 天)
显示 更早的评论
There is a structure with multiple fields and one of them is LOC, for example
n=5;
for i=1:n
S(i).LOC='LL';
end
From this struct, can someone please help me to find:
1) Whether 'LL' exist in the field LOC or not?
2) And how many times it is repeated?
(Note: the above written code is not what Im working on, its just to give some idea.)
0 个评论
采纳的回答
更多回答(1 个)
OCDER
2017-9-8
编辑:OCDER
2017-9-8
I think this works. Not the most efficient, but gets the job done.
n=5;
for i=1:n
S(i).LOC='LL';
end
FoundLL = 0; %Counts how many LL's there in in S structure array
for j = 1:length(S) %Go over every structure in S
Fields = fieldnames(S(j)); %Find the fields in this structure
for k = 1:length(Fields) %Go over every field in S
if strcmp(Fields{k}, 'LOC') %If you have the LOC field name
CurValue = S(j).(Fields{k}); %Get the value of this field
if ~isempty(CurValue) && ischar(CurValue) && strcmp(CurValue, 'LL')
FoundLL = FoundLL + 1;
end
end
end
end
fprintf('Found this many "LL" in "LOC" field of structure S: %d\n', FoundLL)
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!