problem with write to text file
3 views (last 30 days)
Show older comments
Hello everyone,
I have a problem with write data to files, so if you could advise please.
here is the code of my program:
clc, clear
startingEpochs = 100;
for noOfHid=56:75
for n=1:15
noOfIt = startingEpochs*n;
noOfItEnd= 'it';
ending2 = 'hu0.1lr0.1m.mat';
nameToLoad = strcat ('nameSamp_',num2str(noOfIt),noOfItEnd,num2str(noOfHid),ending2);
nameToWrite = strcat('nameSamp_',num2str(noOfIt),noOfItEnd,num2str(noOfHid),ending2,'.txt');
fid=fopen(nameToWrite,'w');
in='name_testing.in';
p=load(in);
p=transpose(p);
tr=nameToWrite;
t=load(tr);
t=transpose(t);
load (nameToLoad),net;
out1=sim(net, p);
fprintf(fid,'%f\n',out1);
end
end
The problem is every time I run the program it gives me this error:
??? Error using ==> fprintf
Invalid file identifier. Use fopen to generate a valid file identifier
Could you please advise how to fix this please?
0 Comments
Answers (3)
G A
on 2 Mar 2012
Changing ownership (permissions) of your destination folder can help
8 Comments
Jason Ross
on 2 Mar 2012
I know what response I'd get from the *NIX folks if I wanted my program to write where it was installed or in places like /etc or /bin. It would likely involve a lot of hearty laughter and/or a steady stare of disbelief. Perhaps a facepalm or two. Then my account would be deleted "by accident" :)
Walter Roberson
on 2 Mar 2012
Don't think I've ever deleted a Unix account "by accident". Had to let some go when bad blocks happened to eat their home directory in the days when the entire system filesystem was on two 5 1/4" floppies, but it didn't take many of those before I became a convert to The Cult Of Backups. And I have this nagging suspicion that if I muse on it for a bit, I might remember the byte-level filesystem layout on my first unix systems... now what was that filesystem magic number again? E3DB ?
Walter Roberson
on 2 Mar 2012
Your fopen() call is failing, and your code is not checking for that possibility.
Side note: you should probably use 'wt' instead of 'w'. That will not affect whether the file can be opened, but will affect how newlines get represented.
12 Comments
Walter Roberson
on 3 Mar 2012
save (nameToWrite),net;
means to start by saving the entire content of the current workspace to the file designated by nameToWrite. Then the output (if any) of the save() command is to be sent to the display, and then "net" will execute as a command if there is no local variable but there is a command by that name (if there is a local variable by that name, nothing will happen because of the semi-colon; without the semi-colon, the value of that variable would have been displayed.)
On the other hand,
save (nameToWrite,'net')
means to save only the variable named "net" to the file designated by nameToWrite, and then to display the output of the save command if there was any.
In the first case, the comma is acting to separate multiple commands on the same line, but in the second case where the comma is in brackets, the comma separates multiple arguments to the same function "save".
Jan
on 2 Mar 2012
After a FOPEN a check is a good programming practice:
fid = fopen(nameToWrite, 'w');
if fid == -1
error('Cannot open file: %s', nameToWrite);
end
In older Matlab releases, this failed, when nameToWrite contains a "\". Using and error ID solved the problem:
error('MyName:Program:Fault', 'Cannot open file: %s', nameToWrite);
2 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!