The title pretty much sums it up. I want to use fopen to create or open a file and it is just returning -1 everytime. It seems like Matlab has not the rights to do that but I don't understand why.
I am administrator, I tried running Matlab as admistrator and I know I am in the right folder.

9 commentaires

dpb
dpb le 18 Fév 2021
Return the option message argument to see what the error returned is...and show us code/error messages in context, don't just describe to us. May be overlooking some clue.
What happens for trying to edit/save a file in editor or writing a file with, say, writematrix that just takes a filename? Do they fail as well?
Theo Y
Theo Y le 18 Fév 2021
It may seem I don't give all details but it is really all I do:
[stuff,msg] = fopen('filename.txt','w')
Result is:
stuff =
-1
msg =
'Permission denied'
I checked my permission parameters on the folder but everythings is fine. Writing is permitted.
dpb
dpb le 18 Fév 2021
"What happens for trying to edit/save a file in editor or writing a file with, say, writematrix that just takes a filename? Do they fail as well?"
Out of curiosity, what does
cd
return?
Theo Y
Theo Y le 18 Fév 2021
D:\Work\Stage
I just tried to create files above that and it works for just D:\Work or D:\
That is really weird considering the authorizations are exactly the same between folders that works and those that don't.
dpb
dpb le 18 Fév 2021
What about
!attrib D:\Work
!attrib D:\Work\Stage
??
Theo Y
Theo Y le 18 Fév 2021
!attrib D:\Work
Gives
D:\Work
and
!attrib D:\Work\Stage
gives
D:\Work\Stage
I feel like I'm missing something here.
Theo Y
Theo Y le 18 Fév 2021
Name: 'D:\Work'
archive: 0
system: 0
hidden: 0
directory: 1
UserRead: 1
UserWrite: 1
UserExecute: 1
GroupRead: NaN
GroupWrite: NaN
GroupExecute: NaN
OtherRead: NaN
OtherWrite: NaN
OtherExecute: NaN
Name: 'D:\Work\Stage'
archive: 0
system: 0
hidden: 0
directory: 1
UserRead: 1
UserWrite: 1
UserExecute: 1
GroupRead: NaN
GroupWrite: NaN
GroupExecute: NaN
OtherRead: NaN
OtherWrite: NaN
OtherExecute: NaN
dpb
dpb le 18 Fév 2021
Indeed, looks strange.
wd='D:\Work\Stage';
[stuff,msg] = fopen(fullfile(wd,'filename.txt'),'w')
??
Theo Y
Theo Y le 18 Fév 2021
Permission denied.

Connectez-vous pour commenter.

 Réponse acceptée

Steven Lord
Steven Lord le 18 Fév 2021

0 votes

Does the file D:\Work\Stage\filename.txt exist when you try to open it for writing? What do these commands return?
filename = 'd:\Work\Stage\filename.txt'
exist(filename) % Does it exist?
fileattrib(filename) % What permissions do the various groups have to this file?

9 commentaires

Theo Y
Theo Y le 18 Fév 2021
It doesn't exist as I try to create it with fopen, which works on some folder but not the ones I want somehow.
dpb
dpb le 18 Fév 2021
Does it show in FileManager I presume?
What about
!cd D:\Work
!rd Stage
!md Stage
then retry fopen()
Theo Y
Theo Y le 18 Fév 2021
"Does it show in FileManager I presume?"
No, because it doesn't exist.
!rd Stage
!md Stage
What those 2 commands do? I can't find information about them on internet and I have other stuff on these folder that I don't want to lose.
dpb
dpb le 18 Fév 2021
The folder, not the file was asking if does exist.
I was presuming if you couldn't write to the folder it would be empty and so was suggesting to remove it and recreate it to see if that would fix something funky in the OS file system.
"rd" and "md" are remove/make directory, respectively. You can't remove a directory that isn't empty so wouldn't have any effect if there are file there.
The above actually would need to be in a .bat file to submiit for the first CD command to have effect of leaving in the \Work subdirectory where need to be for the other two to be effective.
If there are other files in D:\Work\Stage already, I'm guessing they were maybe moved there externally or by some other software process and has left the write permission locked.
At this point, if shutting down and rebooting doesn't cure the problem, I'd probably move those files somewhere else, clean out and remove the directory, then recreate and repopulate it.
Or, just take the expedient of building a new working area and abandoning this one in place and worry about how to fix up whatever is wrong inside the file system later.
Theo Y
Theo Y le 18 Fév 2021
The folder exists.
I'll try moving what is inside of "Stage" folder in a new folder.
I just hope it is just this folder the problem and not all the subfolders.
dpb
dpb le 18 Fév 2021
I'd try a reboot first if you haven't...
It doesn't exist as I try to create it with fopen, which works on some folder but not the ones I want somehow.
Humor me, please, and check. I want to eliminate the case where it shouldn't exist and you are sure it doesn't exist, but because of something that happened earlier (your MATLAB code was supposed to create the file then move it somewhere else or delete it, but MATLAB threw an error before running the code to move or delete the file) it's still present and is in a state where you can't overwrite it.
If this is the case, you might want to create an onCleanup object to close the file regardless of whether the function that created it returned successfully or errored out.
name = fullfile(tempdir, 'abracadabra.txt');
fid = fopen(name, 'wt');
closeme = onCleanup(@() fclose(fid));
fprintf(fid, '%d\t', (1:10).^2);
delete(closeme)
type(name)
1 4 9 16 25 36 49 64 81 100
If I had had this in a function (without the delete call) and the fprintf line threw an error, the closeme object would be deleted when the function exited and would close the file. If the function returned successfully the closeme object would be deleted when the function exited and would close the file.
If MATLAB or your computer crashed or you lost power and so your machine shut down (assuming a desktop with no UPS) then the onCleanup object won't clean up. But at that point you likely have other concerns.
Rik
Rik le 18 Fév 2021
I have heard about an issue where on a cracked version fopen would fail for file extensions that were assigned a program in Windows. The issue would magically appear and disappear, but fopen on files without a recognized extension would always work. I tried to reproduce it so I could send a bug report, but neither of us managed to find the root cause (other than presumably the crack).
So if you're running a cracked version, see if your university or the organization facilitating your internship can provide you with a valid license. (the presumption of you being a student and doing an internship being based on your folder name meaning 'internship' in Dutch)
Theo Y
Theo Y le 19 Fév 2021
So I restarted my laptop and tried the initial command:
>> fopen('D:\Work\Stage\truc.txt','w')
ans =
-1
Interestingly, a popup from Avast was telling me that it blocked an action from Matlab as it tries to create files. I clicked on authorized and did it again:
>> fopen('D:\Work\Stage\truc.txt','w')
ans =
3
It worked!
Very strange to me that it was avast blocking the action all along. I didn't have any popup from Avast yesterday though. It is also very strange that avast protects only this folder from Matlab also. Why not everyone of them?
In any case, in all this strangeness, the problem is solved.
Thank you for taking time replying to me.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by