How to iteratively add elements to a cell array without clearing previous elements?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Stephen23
2019년 3월 15일
편집: Stephen23
2019년 3월 15일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!