I am trying to concatenate two nifti files together in matlab. To do this, I open both nifti files, concatenate the volumes along the last dimension, then save with niftiwrite(volume,‘filename’). However, upon trying to save with niftiwrite, I am repeatedly getting this error:
Error using images.internal.nifti.niftiImage/writeHeader
Cannot open file for writing header.
I can’t find anything about it online, on this forum, or on matlab’s forum. Any ideas?
Does filename include .nii.gz? That may be required. If the file name argument includes the full path, you should make sure that full directory tree exists. But I do not use MATLAB - this can be also be done pretty handily in python with Nibabel/Nilearn if you are not tied to MATLAB.
Can you be more specific about what you mean by an image? It looks like it only expects numeric 3/4d data arrays for the main argument (that is, not image objects or anything extra like metadata), is that what you have?