function MRI_overlay_histogram(mrifilename,figfilename,xlimits) %MRI_overlay_histogram(mrifilename,figfilename[,xlimits]) % %INPUTS: % mrifilename = the name of the mri volume overlay % figfilename = the filename of the png file to output % if empty, no figure will be saved % xlimits = the limits on the x axis (optional) % % This function, given an MRI overlay file in any of the accepted % formats (.nii, .mgz, etc) will output a histogram image % showing the distribution of data values. % Also, min,max,median and mean will be output. % % Note: you must source the freesurfer environment for this % to work, because MRIread is called. d=dir(mrifilename); if isempty(d) disp('ERROR: mri file not found.'); return; end [PATHSTR,NAME,EXT,VERSN] = fileparts(mrifilename); fprintf('Reading %s ...',fullfile(pwd,[NAME EXT])); mri = MRIread(mrifilename,0); v1 = mri.vol(:); v = v1(find(isfinite(v1))); fprintf('Total voxels: %d. ',length(v1)); if length(v1) > length(v) fprintf('%d infinite and %d Nan values excluded.\n',... length(find(isinf(v1))),length(find(isnan(v1)))); else fprintf('All voxels finite (a good thing).\n'); end %close all; %figure hist(v,100); if exist('xlimits') && ~isempty(xlimits) xlim(xlimits); end if exist('figfilename') && ~isempty(figfilename) exportfig(gcf, figfilename, 'format', 'png', 'color', 'cmyk', ... 'height', 6, 'FontMode', 'fixed', 'FontSize', 12) end fprintf('Min...Max: %f ... %f\n',min(v),max(v)); fprintf('Mean...Median: %f ... %f \n',mean(v),median(v));