# MATLAB helpful hacks

### Index Non-Empty Cells in Cell Array

`x = {1,[],[],[]}; find(~cellfun(@isempty,x)) `

### Exiting loop (for / while -> pause) gracefully

(i.e., without “ctrl-C” abort)

For example, when paging thru trials:

```figure('Name','Plot trial','CurrentChar',' ');
for k = 1:length(trial),
...
drawnow; pause;
if get(gcf,'CurrentChar')=='q', % pressing "q" will exist the loop, make sure focus is on the figure!
break;
end
clf;
end```

### Finding indices between two vectors of start and end indices, using arrayfun

Assume we have two vectors, repeated_segments_start_idx, and repeated_segments_end_idx, and we want to find all indices between each pair.

```idx = [repeated_segments_start_idx(segments2remove_idx):repeated_segments_end_idx(segments2remove_idx)] % DOES NOT WORK, OF COURSE!

idx = cell2mat(arrayfun(@colon,repeated_segments_start_idx,repeated_segments_end_idx,'UniformOutput',false)); % WORKS!```

### Maximize figure window

`set(gcf,'units','normalized','outerposition',[0 0 1 1]);`

### Plot an ''imagesc'' with transparent NaNs

```h = imagesc(X,Y,C);

### Shifting colormaps

Sometimes the colormap plots are shifted after printing. To fix this: - in Adobe Illustrator, click on the colormap, release clipping mask, and go to “Object”, “Rasterize”, in the “Resolution” part click on the “Use Document Raster Effects Resolution”.

You have to do it for each of the colormaps.

### Close all windows in Editor

`closeNoPrompt(matlab.desktop.editor.getAll);` 