Search code examples
matlabobjecthandle

How to turn `nans` into `zeros` in objects or handles using indexing


The variable trackWorkspace contains cell structures. The variable intensityDIV is a vector array inside trackworkspace. I want to turn any nans inside intensityDIV into zeros. It is giving me an error saying that:Insufficient outputs from right hand side to satisfy comma separated list expansion on left hand side. Missing [] are the most likely cause.

data = [handles.trackWorkspace.intensityDIV];
if any(isnan(data))
    handles.trackWorkspace(isnan(data)).intensityDIV = 0;
end

Solution

  • handles.trackWorkspace(isnan(data)).intensityDIV creates a comma separated list and to assign values to it, you need to have as many elements on the right hand side as you do in that comma separated list. You only have a single value (0) on the right-hand side which is leading to your error.

    One way to accomplish this would be to use deal to supply a 0 for each element

    [handles.trackWorkspace(isnan(data)).intensityDiv] = deal(0);
    

    This specifies each intensityDiv field as an output and since we only provide one input to deal, it provides this same value to all outputs.