How to update structure and delete entries from structure?
조회 수: 11 (최근 30일)
이전 댓글 표시
Hello,
I have searched the forums and have not found a work around for my issue.
I am using regionprops that is analyzing a series of pictures in a for loop. The output from regionprops is a structure containing the relevant region properties I want. As I am analyzing many pictures (in a for loop) I would like to update a finalResults structure with each iteration of region prop data.
After all images have been analyzed, I do some data analysis that removes certain objects (i.e. is EquivDiam is smaller than 0.5). Here is my pseudo code:
for ii = 1:10
imshow(currentImage);
currentResults = regionprops(currentImage,'Area', 'EquivDiameter');
end
It appears as though MATLAB won't let me concatenate the currentResults structure, with another structure, i.e.
finalResults = (finalResults,currentResults);
as I could do with a simple array. My work around this is to convert the currentResults structure into individual arrays, concatenate the results, and then convert back into a structure, as shown below:
Area = [currentResults(:).Area];
resultsArea = [resultsArea,Area];
EquivDiameter = [currentResults(:).EquivDiameter];
resultsEquivDiameter = [resultsEquivDiameter,EquivDiameter];
finalResults = struct('Area', resultsArea, 'EquivDiameter', resultsEquivDiameter);
This works but is not a very elegant solution. It seems to me very straight forward to want to update a structure within a for loop. Any ideas?
For data analysis as well, I can index the values of Area that are less than a certain threshold, but again, have to to call each individual array inside the structure to delete, i.e.:
finalResults.Area(index)=[];
finalResults.EquivDiameter(index)=[];
Instead of just calling the row number of the structure and deleting the indexed rows from each field of the finalResults structure:
finalResults(index) = [];
Which returns:
Matrix index is out of range for deletion.
There has to be an easier way to work with structures in Matlab.
Any help would be greatly appreciated.
댓글 수: 0
답변 (1개)
Prem Kumar Tiwari
2018년 9월 25일
Hi Mark,
You can do something like the following :
% create an empty variable
finalResults = [];
% keep concatenating individual structs like array
for i = 1:10
currentResults = getCurrentResults();
finalResults = [finalResults currentResults];
end
Here finalResults is a struct array.
Be careful about the following line in the loop above :
finalResults = [finalResults currentResults];
Everytime the currentResult which is concatenated, should be identical to the finalResults structure array, and by that I mean all the structs that are held by finalResults should have exactly same fields.
Now you can very well go ahead and do something like following to delete an entry and that should work just fine.
finalResults(index) = [];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!