Skip to content

Instantly share code, notes, and snippets.

@ofgulban
Created October 31, 2019 13:56
Show Gist options
  • Select an option

  • Save ofgulban/4c2990be3377a391548482374d54b2eb to your computer and use it in GitHub Desktop.

Select an option

Save ofgulban/4c2990be3377a391548482374d54b2eb to your computer and use it in GitHub Desktop.
Nifti to Brainvoyager's fmr file format conversion.
% Convert FMR to NII for files converted from FMR to NII before.
%
% Requires Neuroelf <http://neuroelf.net/>.
clc; clear all;
PATH_OUT = '/patt/to/output_folder';
SUFFIX = 'converted'
% set nii names created after spm or fsl manipulation
PATH_NII{1}=['/path/to/file1.nii.gz'];
PATH_NII{2}=['/path/to/file2.nii.gz'];
% set an fmr from the same subject to copy the header information
PATH_FMR{1}=['/path/to/file1.fmr'];
PATH_FMR{2}=['/path/to/file2.fmr'];
%% Convert nii to fmr
disp('Converting nii files to fmr...')
for i=1:length(PATH_FMR);
temp_nii=xff(PATH_NII{i});
temp_fmr=temp_nii.Dyn3DToFMR;
% Get correct fmr header
header_fmr=xff(PATH_FMR{i});
header_fmr.Slice.STCData=temp_fmr.Slice.STCData;
% Save
[i_dir, i_name, ~] = fileparts(PATH_FMR{i});
out_path = fullfile(i_dir, [i_name '_' SUFFIX '.fmr']])
header_fmr.SaveAs(out_path);
disp([out_path ' created.'])
end
disp('Done.')
@arnaublanco

arnaublanco commented Oct 31, 2020

Copy link
Copy Markdown

Hello, I tried your code and for some reason, it shows an error.

Unable to resolve the name hdrs.F.

Error in hdr_Dyn3DToFMR (line 113)
fmrc.FirstDataSourceFile = hdrs.F;

Error in xff/subsref (line 129)
[varargout{1:nargout}] = feval(tfm{6}, xo, fargs{:});

Error in nii_to_fmr (line 22)
temp_fmr=temp_nii.Dyn3DToFMR;

The edited the code according to my file structure.

image

That is, I only changed the first part of the code: the path.

PATH_OUT = '';
SUFFIX = 'converted';

% set nii names created after spm or fsl manipulation
PATH_NII{1}=['test.nii'];
PATH_NII{2}=['test2.nii'];

% set an fmr from the same subject to copy the header information
PATH_FMR{1}=['test.fmr'];
PATH_FMR{2}=['test2.fmr'];

Thanks!

@ofgulban

ofgulban commented Nov 2, 2020

Copy link
Copy Markdown
Author

Hi @arnaublanco , thanks for reporting this. As far as I can see, you do not have the fmr files. I was using this script to only replace headers after converting fmr files to nii within BrainVoyager. You might be rather looking for a direct conversion script, so if I am understanding correctly, have a look at this one:
https://gist.github.com/ofgulban/2677029103bc2433fcca1c2bf3042711

@arnaublanco

Copy link
Copy Markdown

Yes, this is exactly what I was looking for. Thanks a lot!

@arnaublanco

arnaublanco commented Nov 3, 2020

Copy link
Copy Markdown

Just out of curiosity, what is in the header information? Because I'm now using your other script, and I'd like to know exactly what information is lost.

Thanks.

@ofgulban

Copy link
Copy Markdown
Author

The header includes the voxel dimensions, affine transformation matrix and several other useful information about the image. Some of these fields are converted to BV formats correctly but some others are not used in BV or just not have an equivalent field in BV file formats. It is always good to check the BV headers after any conversion (e.g. checking whether the voxel dimensions are correct). You can inspect these fields in File -> Documents Properties menu in BV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment