How to write text file WITHOUT starting a new line
44 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/633805/image.png)
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: ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/636935/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/636935/image.png)
Thank you for the help!
Risposte (1)
Jan
il 28 Mag 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
2 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!