Search code examples
matlabtextdelimitercsvexport-to-text

Add a double space as a delimiter


I have a huge amount of large text files that need to be reformatted so that they can be read by FEAT (an MRI analysis program). FSL reads text files with a double space as a delimiter. I've been using matlab to add a row of information that I need to the txt files and i'm trying to get it to export the resulting array as a text file with a double space delimiter but I can't figure out how to do it. dlmwrite only allows for single characters to be used as delimiters.

Any ideas?


Solution

  • You could use your own version of dlmwrite. For example:

    function my_dlmwrite(filename, m, delimiter_string, sigfigs)
    if(~ischar(delimiter_string))
        error('delimiter string should be a string');
    end
    if(~isnumeric(m) || ~(isreal(m)))
        error('matrix should be a real matrix of doubles')
    end
    if(~isscalar(sigfigs))
        error('the number of significant figures to print should be a scalar');
    end
    if(sigfigs < 5)
        warning('do you really want fewer than 5 significant significant digits?');
    end        
    
    fid = fopen(filename ,'W');  %open for writing and dont flush for better performance
    
    [n_rows, n_cols] = size(m);
    
    for(i=1:n_rows)
        fprintf(fid, '%.*g', sigfigs, m(i, 1));        
        for(j=2:n_cols)
            fprintf(fid, '%s%.*g', delimiter_string, sigfigs, m(i, j));
        end
        fprintf(fid,'\n');
    end
    
    fclose(fid);