MATLAB: Customise a string and output it into a new file with x number of words per line

stringtext file

I have a string of words (let's say a text from Sherlock Holmes), how do I write this string to an output file with x number of words per line?
For example, if i have a very long string, and i only want 15 words per line outputed to a new file, how do i do this?
Thanks you and any help would be really appreciated.
Best wishes!

Best Answer

  • it's a long method but worked for me
    s = 'My name is mango ';
    for i =1:10
    s = [s s];
    end
    x=split(s);
    x=reshape(x(1:15*floor(length(x)/15)),15,floor(length(x)/15));
    x(16,:)={'\n'};
    file_id=fopen('vava.txt','w');
    fprintf(file_id,strjoin(x));
    fclose(file_id);