How to iteratively add elements to a cell array without clearing previous elements?

2 views (last 30 days)
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?

Answers (1)

Stephen23
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
Avery McGuirt
Avery McGuirt on 16 Mar 2019
This would be consistent with what's happening, but as far as I know, I've kept all of my functions consistent in updating the handles structure. If it helps, here is the full code for the individual function in question.
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ymax = max(handles.dFF(:,handles.z)) ;
axis(handles.axes2, [0 600 0.9 ymax]);
set(handles.axes2, 'XScale', 'linear');
set(handles.axes2, 'box', 'on');
%PEAKFINDER Noise tolerant fast peak finding algorithm
clear 'PeakLoc' ;
clear 'PeakMag' ;
sel = (max(handles.dFF(:,handles.z))-min(handles.dFF(:,handles.z)))*str2double(get(handles.edit1, 'String')) ;
PeakFinderGUI(handles.dFF(:,handles.z), sel, str2double(get(handles.edit2, 'String')), str2double(get(handles.edit3, 'String')), strcmp(handles.edit4,'true'), false, handles) ;
[PeakLoc, PeakMag] = PeakFinderGUI(handles.dFF(:,handles.z), sel, str2double(get(handles.edit2, 'String')), str2double(get(handles.edit3, 'String')), strcmp(handles.edit4,'true'), false, handles) ;
handles.PeakLoc{handles.z} = PeakLoc ;
handles.PeakMag{handles.z} = PeakMag ;
assignin('base', 'PeakLoc', handles.PeakLoc) ;
guidata(hObject, handles) ;
Stephen23
Stephen23 on 16 Mar 2019
Edited: Stephen23 on 16 Mar 2019
Then you will need to use the debugging tools to investigate when this occurs: set some breakpoints, run your GUI code, and then check all of the relevant variables.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!