problem with write to text file
Mostra commenti meno recenti
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?
Risposte (3)
G A
il 2 Mar 2012
0 voti
Changing ownership (permissions) of your destination folder can help
8 Commenti
H. S.
il 2 Mar 2012
G A
il 2 Mar 2012
http://www.addictivetips.com/windows-tips/windows-7-access-denied-permission-ownership/
H. S.
il 2 Mar 2012
Jason Ross
il 2 Mar 2012
Before you just go and open up permissions on a folder, look at where you are writing and see if it makes sense. Microsoft didn't come up the permissions they did to hassle people -- they did it because there are parts of the system where it doesn't make good sense for programs to be writing data out -- for example, the "Program Files" directory and the "C:\Windows\System32" directory -- these locations are not guaranteed to persist through a system update or a program reinstall, so if you are dumping output files there, then you can lose your data when that occurs.
You should probably look at changing the directory to somewhere you are supposed to be writing - for example, your user directory (usually c:\users\username) or TEMP (denoted by the TEMP environment variable). You might also have HOMEDRIVE, HOMESHARE and HOMEPATH populated, as well.
G A
il 2 Mar 2012
Yes, of course, better to check first with notepad if writing is permitted.
Walter Roberson
il 2 Mar 2012
"Microsoft didn't come up the permissions they did to hassle people" -- right, the permissions were just the MacGuffin to justify the hassle ;-)
Jason Ross
il 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
il 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
il 2 Mar 2012
0 voti
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 Commenti
H. S.
il 2 Mar 2012
Walter Roberson
il 2 Mar 2012
[fid,message] = fopen(nameToWrite,'wt');
if fid < 0
fprintf(2,'Open failed for "%s" because "%s"\n', nameToWrite, message);
next
end
Jan
il 2 Mar 2012
While the 'w' option of FOPEN let the Unix-linebreak CHAR(10) appear in the file for FPRINTF('\n'), the option 'wt' writes the DOS-linebreak CHAR([13, 10]). The DOS-linebreak is no advantage, except if you love to work with the program "Editor" shipped with Windows. All other editors (e.g. Matlab's, WordPad, Word, Emacs, vi and Alpha under MacOS9) understand the Unix linebreak also. Matlab's editor uses Unix linebreaks also as default for some years now.
Therefore I do not see the benefit of using 'wt'.
H. S.
il 2 Mar 2012
Walter Roberson
il 2 Mar 2012
MS NotePad also needs CR LF.
'wt' does not exactly mean to use CR LF: it means to use the convention of the system the program is executing on. On Unix systems, there is no difference in the file format; on Windows it uses CR LF.
Using 'wt' for text files costs almost nothing, but avoids problems. And for those of us whose code might be copied by people who do not know about line terminators and might unknowingly use NotePad, it is good practice for us to write code that will cause people the least problem if our code is copied or our style is copied.
I also answer a fair number of the questions having to do with serial port communication, or device communications (e.g., data acquisition), where getting the right terminator is often crucial.
Stock Questions for device communication:
#1: Is the baud rate set right?
#2: Is the line terminator set right?
#3: Is the cable plugged in to the right places?
#4: Is the cable wired right?
#5: Is the device plugged in and turned on?
#6: Is hardware flow control set right?
#7: Do both sides agree on the voltages being used, RS232 vs TTL?
#8: Do both sides agree on the clocking protocol?
Walter Roberson
il 2 Mar 2012
load (nameToLoad, 'net')
Jan
il 2 Mar 2012
Of course, "NotePad" is the correct name, not "Editor". Is there any other program besides NotePad, which fails for Linux linebreaks?
When getting the right terminator is crucial, this choice should not be done implicitely in FOPEN, because the result depends on the OS then. When I want to be sure, that a stream gets CR LF in every case, I use:
f=fopen(File, 'w'); fprintf(f, ['message', char([13, 10])]);
I think, the dependency on the platform is a source of problems and the behaviour of ^Z, Tab etc as well as non-ASCII characters can be rather confusing - although it is well defined. If Linux can live without the 'wt' format, I can also.
But, Walter, we have discussed this before and as far as I can see, we have both good arguments, both of us prefer the own arguments, and nobody else took part on our discussions. Therefore I suggest:
'wt' can avoid or cause troubles. Use it appropriately.
Walter Roberson
il 2 Mar 2012
The 't' is not just ignored in Linux. A file or device opened with 't' results in a text stream, which has different properties with regards to the seek / tell operations, and with regards to default buffering behavior, and with regards to some operations of pseudo-terminals (ptty's).
H. S.
il 3 Mar 2012
H. S.
il 3 Mar 2012
Walter Roberson
il 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".
H. S.
il 3 Mar 2012
Jan
il 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);
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!