How to write text file WITHOUT starting a new line

36 ビュー (過去 30 日間)
Daniele Venanzetti
Daniele Venanzetti 2021 年 5 月 28 日
コメント済み: Daniele Venanzetti 2021 年 5 月 31 日
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 件のコメント
Stephen23
Stephen23 2021 年 5 月 31 日
編集済み: Stephen23 2021 年 5 月 31 日
"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
Daniele Venanzetti 2021 年 5 月 31 日
It solved my problem! thank you!

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2021 年 5 月 28 日
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 件のコメント
Daniele Venanzetti
Daniele Venanzetti 2021 年 5 月 29 日
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"
Walter Roberson
Walter Roberson 2021 年 5 月 29 日
You are replying here to @Jan not to me ;-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by