How to iteratively add elements to a cell array without clearing previous elements?
2 views (last 30 days)
Show older comments
I am writing GUIDE code to identify peaks in a signal and return locations/magnitudes. I am trying to add an element to a cell array for every iteration through a different data trace (indexed by 'z'). PeakLoc and PeakMag are vectors returned from a peakfinding function external to the GUI code, and are different sizes for each new iteration.
[PeakLoc, PeakMag] = PeakFinder(%input data)
handles.PeakLoc{handles.z} = PeakLoc ;
handles.PeakMag{handles.z} = PeakMag ;
However, every addition clears previous entries to the cell array. For example, after processing the first trace (handles.z = 1):
assignin('base', 'PeakLoc', handles.PeakLoc) ;
PeakLoc =
1×1 cell array
{14×1 double}
While stepping through an additional trace (handles.z = 2) returns:
PeakLoc =
1×2 cell array
{0×0 double} {20×1 double}
The code should ideally return both the initial 14x1 vector as well as the new 20x1 vector. Any idea where this is going wrong?
0 Comments
Answers (1)
Stephen23
on 15 Mar 2019
Edited: Stephen23
on 15 Mar 2019
I suspect that you forgot to actually store the handles data after you made changes to it. The handles structure inside the callback is a local copy, when you make any changes (e.g. adding fields or data) to it then those changes are not reflected in the original structure stored in the GUI figure. This means that all changes you make are simply discarded at the end of the callback function, unless you explicitly update the figure's copy with the local copy. You can use guidata to do that:
Simply add this at the end of any callback within which you change the handles data:
guidata(hobj,handles) % hobj is the first input to the callback.
2 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!