problem with write to text file

4 次查看(过去 30 天)
H. S.
H. S. 2012-3-2
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?

回答(3 个)

G A
G A 2012-3-2
Changing ownership (permissions) of your destination folder can help
  8 个评论
Jason Ross
Jason Ross 2012-3-2
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
Walter Roberson 2012-3-2
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
Walter Roberson 2012-3-2
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 个评论
Walter Roberson
Walter Roberson 2012-3-3
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".
H. S.
H. S. 2012-3-3
Dear Mr Roberson,
Thank you for your kind advice.
Regards,
H. S.

请先登录,再进行评论。


Jan
Jan 2012-3-2
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 个评论
H. S.
H. S. 2012-3-2
Thank you for your kind advice. I do have a question please.
As you can see, I was trying to write a program which can load and write multiple files. The problem occurs when I am doing so.
Please see these lines below (from the full programming above)
1. load (nameToLoad),net;
2. load nameSamp_200it54hu0.1lr0.1m.mat net;
Problem is they yielded different outputs.
The first line : load (nameToLoad),net;
yielded the result like this:
out1 <204x1632 double>
p <864x1632 double>
which is "WRONG"
whereas: load nameSamp_200it54hu0.1lr0.1m.mat net;
yielded the result like this:
out1 <204x408 double>
p <864x408 double>
which is "RIGHT"
Could you please kindly advise how to use load (nameToLoad),net
correctly so I could load and write multiple files with strcat method, so that I don't have to input the name to load and write manually which will take forever.
Please kindly advise.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by