fprintf leaving remainder of line

I am reading a txt file into matlab using fopen then textscan. the text is reading into a single cell that 43x1 (So if I wanted to get line 43 I would enter File{1}(43,1)). I change some of the lines and rewrite the file as such
frewind(fileID)
for row=1:length(file{1})
fprintf(fileID,'%s\n',file{1}{row,1});
end
When I reopen the text file it leaves a portion of line 43 in a new line 44
(line 43) There is Ketchup on my hot dog
(line 44) on my hot dog
The rest of the text file is written correctly, what is causing this to happen? even if there is a line 44 generated, if it was blank it would be fine

댓글 수: 9

dpb
dpb 2018년 6월 22일
That has to do with what you didn't show; HOW you " change some of the values". It appears you inserted a line or an additional newline but we can't see what was done, specifically, to know.
this is how I am changing some of the lines
s1 = strsplit(char(file{1}(lineA,1)),' ');
s1{1,2} = 'new string';
s11 = strjoin(s1);
file{1}(lineA,1) = cellstr(s11);
dpb
dpb 2018년 6월 22일
Well, we still can't see what's actually in any of the files either before or after...how about attach a (small) sample case that illustrates the issue?
Keith Holmlund
Keith Holmlund 2018년 6월 22일
편집: Keith Holmlund 2018년 6월 22일
In file it originally reads (file{1}(36:43,1))
(line 36) the deck is printed red
(line 37) jim bought a new grill
(line 38) the watermelon is in the cooler
(line 39) there are two tables in the backyard
(line 40) the sky is cloudy
(line 41) rain is the forecast
(line 42) sally bought hot dogs
(line 43) There is Ketchup on my hotdog
I change line line 36 and line 40 using a for loop using the code in the last comment, afterward file{1}(36:43,1) should look like
(line 36) the deck is printed blue
(line 37) jim bought a new grill
(line 38) the watermelon is in the cooler
(line 39) there are two tables in the backyard
(line 40) the sky is clear
(line 41) rain is the forecast
(line 42) sally bought hot dogs
(line 43) There is Ketchup on my hotdog
I don't change the string in file{1}(43,1). there should be no change in the length(file{1})
"there should be no change in the length(file{1})"
Ah, but there can be...length() isn't a very reliable function; particularly with character data to determine the number of rows (lines) in an array; it is max(size(x)).
You then compound the problem by using the {} on file{1} which returns the character array inside the cell, not the cell itself.
Use
nRow=size(file,1);
to obtain the number of rows/records/lines in the cellstr array rather than the size of the strings inside the cell.
There's still an issue perhaps of an extra newline but it looks like your problem is probably related to the above.
Keith Holmlund
Keith Holmlund 2018년 6월 22일
thanks for all of input but the issue persits. I read in text file using textscan which creates a 1x1 cell array, when this cell is expanded is a 43x1 character array. I am able to go in and change some of the lines in the character array, such as line 5,17,and 30. If I were to view this character array before trying to write to file, it looks as it should. I am trying to write the changed character array to file using fprintf. Using the code shown in the original question, I am getting last part of line 43 in the character array to show up in a new line (line 44). If I were to open the text file again in matlab, the character array would have a dimension of 44x1. I am not changing line 43 of the character array. I don't know why it is doing what it is doing,
dpb
dpb 2018년 6월 22일
But you do change the array file and recompute the limit on the for loop from it dereferencing the content not looking at the size of the cell array when you write the new file.
Set a breakpoint and step through the code and I strongly suspect you'll find the logic error.
going through it, the following ended up doing the trick
frewind(fileID);
for row=1:size(file{1},1)
fprintf(fileID,'%s\n',file{1}{row,1});
end
dpb
dpb 2018년 6월 22일
Indeed.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 6월 22일

1 개 추천

If you overwrite a text file by a shorter string than the original, the remaining characters are not cropped. To reduce the file size, you need an extra tool like FEX: FileResize . You cannot do this in Matlab without recreating the file.

댓글 수: 1

Walter Roberson
Walter Roberson 2018년 6월 22일
This is the correct answer. MATLAB does not provide any way to make a file shorter. The POSIX standard library defines I/O in the middle of a file as overwriting existing contents without truncating the file unless the user specifically asks for the file to be truncated (an action which there is unfortunately no MATLAB interface for.)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

릴리스

R2017b

태그

질문:

2018년 6월 22일

댓글:

2018년 6월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by