Problem writing text to file with fprintf

Hi, So.. I have a 727x1 cell array: A, that I'm trying to write to a file called 'norm1.avl' in the following manner:
fid = fopen('norm1.avl', 'w');
fprintf(fid, '%s\n' A{:});
fclose(fid);
Why is this not working? I am seriously going crazy here. Can't find the error. Thanks in advance!

3 Comments

For the format in "fprintf" it seems like A has strings in it's elements (something like A = {'a', 'b', 'c', 'd'}). Your code works if the file is named 'norm1.txt'. The name 'norm1.avl' also writes the file whitout any errors, the only problem seems to be how to open the file (in windows).
You have not indicated what problem you are observing?
Stephen23
Stephen23 on 11 Feb 2016
Edited: Stephen23 on 11 Feb 2016
and how are you observing these problems? Are you double-clicking on the file and it then opens in an unknown program? Are you using (awful) Notepad to view the file? How are you checking or using this file?

Sign in to comment.

Answers (3)

What if you try it this way:
fid = fopen('norm1.avl', 'wt');
for row = 1 : length(A)
fprintf(fid, '%s\n' A{row});
end
fclose(fid);
All cells in A contain strings, right?

3 Comments

Yes exactly, in the code I have already converted A to strings. But this did not work, so I'm gonna show the part of the code concerned now from the beginning:
A = regexp( fileread('norm1.avl'), '\n', 'split');
y=7;
z=4;
A=A';
string=A{105};
a=strread(string);
a(2)=y;
a(3)=z;
a=num2str(a);
a=cellstr(a);
A(105)=a;
fid = fopen('norm1.avl', 'wt');
for row = 1 : length(A)
fprintf(fid, '%s\n', A{row});
end
fclose(fid);
or as I orignally ended it:
fid = fopen('norm1.avl', 'w');
fprintf(fid, '%s\n' A{:});
fclose(fid);
EDIT: It is working if I am naming the file 'norm1.txt' instead. This is not acceptable for the purpose, but maybe this can help you help me! :(
I don't know how fileread() works, but it looks like maybe it keeps the file open so you can't open it again with fopen().
I'd be more inclined to blame the issue on files permissions or on an antivirus than on fileread.
That would be a very bad bug if fileread left open file handles.

Sign in to comment.

First, see if changing the fopen call to:
fid = fopen('norm1.avl', 'wt');
specifying that the permission is to ‘write text’.
Second, if that doesn’t work, before you do the fopen call to open the ‘norm1.avl’ file for writing, insert this code snippet:
fIDs = fopen('all');
for k1 = 1:length(fIDs)
open_filename{k1} = fopen(fIDs(k1));
end
The ‘fIDs’ variable will have the file ids of all open files, and the ‘open_filename’ will have the full paths and names of all the open files. It’s possible that fileread has not closed the file.

2 Comments

Maybe try
fclose('all');
if you're certain that there is nothing you want or expect to be open.
Note that specifying 't' in the fopen call only changes the way matlab translates '\n'. It should have no effect on whether the fopen succeeds or not.

Sign in to comment.

Have you checked that the file you're trying to modify actually has write permissions, and is not read only?
Does fileattrib('norm1.avl') returns UserExecute: 1 ?
Have you looked at what ferror(fid) returns after the fopen fails (assuming that is the problem, you still haven't told us what the actual problem is).

Categories

Asked:

on 11 Feb 2016

Answered:

on 11 Feb 2016

Community Treasure Hunt

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

Start Hunting!