How to update structure and delete entries from structure?
16 次查看(过去 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!