How to write text file WITHOUT starting a new line
Show older comments
Hi Everyone,
I am trying to use either fwrite or fprintf to write data (mostly strings) in a text file but my script automatically starts a new line (maybe because the string is too long to fit in one line). Few comments:
- I am taking data from an excel file
- writecell() and writetable() gave me the same error so I wanted to do it as basic as possible but without success
for i = 1 : length_rows
for j = 1 : length_columns
MCI_excel{i, j} = string(MCI_excel{i, j});
MCI_excel{i, j} = append(MCI_excel{i, j},...
blanks(max_length_array(j) - length(MCI_excel{i, j})));
size_matrix(i, j) = length(MCI_excel{i, j});
fprintf(fid, '%s', ' | ');
fprintf(fid, '%s', MCI_excel{i, j});
if j == length_columns
fprintf(fid,'\n');
end
end
end
Here below you can see the length of each string in the cells.

So, when I try to write the first row, everything runs smoothly till I try to write cell(1,8) and after about 100 characters out of 413 it automatically starts a new line.
How can I force my script to not start a new line? is it a problem of rows that are too long?
UPDATE:
Following @Mathieu NOE nad @Jan inputs, I made some strides. I found out that changing the text the problem disappears. In my file, my script starts a new line in this occasions:
- at "discussed." in the string " ...but can be discussed.Format of ...". Error disappears if I turn it into " ...but can be discussed. Format of ..." (with a space after the period).
- at "Dry: 102%" in the string "Dry: 102% Wet: 55%". I can't find a way to change the line to make the error disappear.
- In other points but lines are different from the ones before.
UPDATE:
Following @Walter Roberson input I got rid of a condition because always true. Moreover, I decided to convert everything to a string instead of char (new code is above). I found the error but I don't know what it is: in my table this symbol ↵ appears different times during my conversion and my text starts a new line there everytime. It's the first time I meet it but I want to get rid of it. Do you know how I can do this?
this is a really small picture of what I see: 

Thank you for the help!
10 Comments
Mathieu NOE
on 28 May 2021
hello
maybe you could share a excel file to let us test your code
tx
Stephen23
on 28 May 2021
"...after about 100 characters out of 413 it automatically starts a new line."
How are you checking this?
Have you disabled line wrapping in the text editor that you are using?
Walter Roberson
on 28 May 2021
if length(MCI_excel{i, j}) <= max_length_array(j)
Could you confirm that if that is false that you do not want the
if j == length_columns
fprintf(fid,'\n');
end
to be done?
Daniele Venanzetti
on 29 May 2021
Daniele Venanzetti
on 29 May 2021
Daniele Venanzetti
on 29 May 2021
Walter Roberson
on 29 May 2021
That block of code refuses to print the new line if length(MCI_excel{i, j}) > max_length_array(j) .
If I understand the code correctly, if the current field is wider than the defined width for the field, you not only skip outputing the field, but you also skip the potential newline.
At the very least you need comments explaining what you want to have happen if the current field is wider than the defined width.
Daniele Venanzetti
on 31 May 2021
"So, do you know what that symbol means..."
Your text apparently contains newline characters.
Whether the code is stored as character or string is irrelevant.
"... and how to get rid of it?"
str = strrep(str,char(10),'')
Daniele Venanzetti
on 31 May 2021
Answers (1)
Jan
on 28 May 2021
What is the contents of MCI_excel{i, j}? If the strings contain e.g. path names including "folder\next", the \n is interpreted as line break. Try this instead:
fprintf(fid, '%s', MCI_excel{i, j});
% ^^^^
Matlab does not decide to insert a linebreak, because "the line is too long". Without an explict instruction to do so, Matlab does not insert anything.
A simplification:
for i = 1 : length_rows
for j = 1 : length_columns
...
% Not here
% if j == length_columns
% fprintf(fid,'\n');
% end
end
end
% But here:
fprintf(fid,'\n');
end
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!