Write_mrtrix.m error using fprintf

I wrote a script in python, using matlab engine to be able to use write_mrtrix.m and read_mrtrix.m. This works perfectly fine when running in jupyter notebook, no problems at all. I want to make it easy to use for others by turning it into a python file that can be run from the command line, also specifying input and output files in the command line. But when I run it from the command line I get this error:

Error using fprintf
Function is not defined for ‘cell’ inputs.
Error in write_mrtrix (line 78)
fprintf (fid, ‘\ncomments: %s’, image.comments)

I don’t know much about matlab, I have tried commenting out that part of the write_mrtrix.m code but using it then created a useless mrtrix file. I have also tried turning the mrtrix files into nifti files and using Nibabel to import the files to python, also didin’t work because of too large dimensions (498511, 1, 1). It is a fixeldata file.

read_mrtrix gives a lot of ‘unknown key’ when I run it, but other than that it seems to work.

Hi Karla,

If I had to guess, I’d say that the “comments” field of your loaded image is being read as a list of strings rather than a single text string, and thus the fprintf() function can’t be executed directly on it. Handling of the “comments” field & other non-compulsory header entries was altered (though quite some time back), so it’s possible that maybe the matlab functions haven’t been updated appropriately? I don’t see why commenting out that line would produce a useless file though, it should just give a file with no comments in it…

I have also tried turning the mrtrix files into nifti files and using Nibabel to import the files to python, also didn’t work because of too large dimensions (498511, 1, 1).

The issue here is that if an image dimension exceeds 65535, then MRtrix3 is forced to use NIfTI-2 (mrinfo should report this), which I suppose Nibabel must not yet support.

read_mrtrix gives a lot of ‘unknown key’ when I run it, but other than that it seems to work.

This is related to the altered handling of non-compulsory header entries. These should be safe to ignore, though the warning should probably be removed.


Also note that with sufficient agitation, @dchristiaens will hopefully add native .mif image read-write functions for Python in the next update :+1:

1 Like