3

I want to change ',' character to '\n' and save it to the text file

All files are in this format:

546,234,453,685,.....,234

I want to make it like:

546

234

453

685

...

234

My initiation to this problem is like this:

fid=fopen(files{i});
strArr=fscanf(fid,'%s');

newstrArr=strrep(strArr,',','\n');

% Take each .txt input
for j=1:length(newstrArr)  
   Array=[Array newstrArr(j)];   
endfor

Let me explain step by step:

1st I open the current text file

 fid=fopen(files{i});

2nd I find the strings in text file

strArr=fscanf(fid,'%s');

Please Note that you can't replace %s with %d. (Correct me if I am wrong)

3rd I replace commas with newline character

newstrArr=strrep(strArr,',','\n');

4th I add each character to a new array with for loop

for j=1:length(newstrArr)  
   Array=[Array newstrArr(j)];   
endfor

However When I display, using;

disp(Array);

I have this output

How can I properly replace the commas with newlines?

Regards

2
  • 1
    If you are on GNU/Linux this would be a job for "tr" or "sed"
    – Andy
    Commented Dec 11, 2016 at 20:33
  • @Andy "tr" or "sed" , never heard before, I will definitely search and learn about it, thanks for knowledge
    – Ahx
    Commented Dec 11, 2016 at 20:40

3 Answers 3

6

The issue is that you are inserting a literal '\n' (the characters \ and n) and not a newline character. This is because in Octave, a single-quote enclosed string ignores escape sequences. If you want Octave to respect escape sequences you could use a double-quoted string which will convert \n into a newline.

strrep(strArr, ',', "\n");

Or if you want your code to be MATLAB-compatible, you'll want to instead use char(10) (an actual new-line character). This is because MATLAB does not have double-quote enclosed strings.

output = strrep(strArr, ',', char(10));

Another option would be to split your input at the , and use sprintf to add the newlines (it'll treat \n as a newline)

values = strsplit(strArr, ',');
output = sprintf('%s\n', values{:});

If you just want to save each entry to a new line in a file, you can use fprintf instead.

values = strsplit(strArr, ',');
fout = fopen('output.txt', 'w');
fprintf(foug, '%s\n', values{:});
fclose(fout);
1
  • Definitely ' char(10) ' improved my solution. You are the man.
    – Ahx
    Commented Dec 11, 2016 at 20:30
0

If you really just want to replace "," with newline simply do

in = fileread ("yourfile");
out = strrep (in, ",", "\n")
out = 546
234
453
685
234

Btw, see the difference between "\n" (in GNU Octave a newline) and '\n' (literally \n)

1
  • I couldn't understand you answer but I do it following way newstrArr=strrep(strArr,',',char(10)); and it gave me an input like 54\n234\n...........
    – Ahx
    Commented Dec 11, 2016 at 20:31
0

Another option is to use regexprep(), this has the advantage of being MATLAB compatible. Assuming that the newline convention you want is \n, then

regexprep('123,456,789',',','\n')

ans = 123
456
789

When output to a file via fprintf() the result looks like

123
456
789

provided the text editor understands the newline convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.