How to write text file WITHOUT starting a new line

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:
  1. 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).
  2. 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.
  3. 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

hello
maybe you could share a excel file to let us test your code
tx
"...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?
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?
@Mathieu NOE I can't share the original excel because there is sensitive data in it. Anyway, I built a dummy excel and my algorithm works as expected. I really suspect that there must be something wrong in the text itself.
With the original file, my algorithm start a new line at "discussed." in the string " ...but can be discussed.Format of ...".
I changed the line in " ...but can be discussed. Format of ..." (with a space after the period) and the algorithm works well without starting a new line anymore but it does many other times somewhere else in the code.
I update my line of code:
fprintf(fid, MCI_excel{i, j});
in:
fprintf(fid, '%s', MCI_excel{i, j});
As @Jan proposed but the result is the same.
Yes, line wrapping is disabled and my text editor is notepad.
That line of code is necessary because when I printed all the cells in the first row, I need to start a new line.
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.
You understand correctly and I noticed that line of code is useless because always true (I have just removed it). By the way, I found the error but I still don't know how to solve it. Instead of turning everything to a char, I turn everything to a string and this symbol ↵ appears exactly where my script starts a new line. So, do you know what that symbol means and how to get rid of it? I will make an update about this.
P.S. Sorry for tagging you under jan's answer :)
"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),'')

Sign in to comment.

Answers (1)

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

2 Comments

This doesn't solve the problem but I think you raised a good point blaming the text itself.
With the original file, my algorithm start a new line at "discussed." in the string " ...but can be discussed.Format of ...".
I changed the line in " ...but can be discussed. Format of ..." (with a space after the period) and the algorithm works well without starting a new line anymore.
the problem is "d.F" and the error vanishes if I turn it into "d. F"
You are replying here to @Jan not to me ;-)

Sign in to comment.

Categories

Products

Release

R2019b

Asked:

on 28 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!