views:

61

answers:

2

Hi, I am using Matlab to create a new file by calling

fid = fopen(filename,'w')

since filename doesn't exist, it should create a new file and give me a valid file descriptor. Instead it returns -1. If I run the code again however, I get fid = 3.

This is being run on ubuntu but it apparently works fine on windows and I can't figure out why.

-Mike

+2  A: 

not sure if this helps, but note that if the folder doesn't exist, fopen with 'w' can't create the file and so returns -1.

second
i'm fixing someone elses code, what command creates folders?
msandbot
matlab has its own mkdir command
second
A: 

You should check out the two-output-argument form of fopen in the doc here. This allows you to do stuff like

[fh, failmessage] = fopen( fname, 'wt' );
if fh == -1
    error( 'Failed to open %s: %s', fname, failmessage );
end
Edric