Is there a null device in Matlab?
Asked Answered
S

2

7

I'm trying to find an easy way to turn logging to a text file on and off much like the example seen here in Python. Their solution was to use a valid file name where logging is desired and to use the null device 'dev/null' otherwise. They're using redirection but I wish to use fopen.

Is there some way to do something like the following, which word work for Unix or Windows systems if that matters.

nullFID = fopen('/dev/nul', 'w')

The script I'm altering could benefit greatly with detailed logging in certain breaking scenarios where we want to pull up a fine level of detail for a handful of files but that would otherwise grow too large for the batch processing the script normally targets.

Semiotic answered 4/2, 2016 at 21:57 Comment(0)
S
4

Looks like this can be done, someone posted an answer on MathWorks using a similar term of "null file". Not sure if that's valid terminology but could be a shortening of "null device file".

The way to reference the null device is OS dependent...

nullFID = fopen( 'NUL:'    ); % Windows
nullFID = fopen('/dev/null'); % UNIX

And it also appears the reference varies from Matlab releases. Consider the following script

ver
nullFID = fopen( 'NUL:' , 'w' )
fprintf( nullFID , '12345' )
nullFID = fopen( 'NUL:'  )
fprintf( nullFID , '12345' )
nullFID = fopen('NUL' , 'w' )
nullFID = fopen('NUL' )

And the following output generated from two versions of Matlab...

R2011b

>> ver
-------------------------------------------------------------------------------------
MATLAB Version 7.13.0.564 (R2011b)
MATLAB License Number: xxxx
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)
Java VM Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
-------------------------------------------------------------------------------------
MATLAB                                                Version 7.13       (R2011b)

>> nullFID = fopen( 'NUL:' , 'w' )
nullFID = 119

>> fprintf( nullFID , '12345' )
ans = 5

>> nullFID = fopen( 'NUL:' )
nullFID = 120

>> fprintf( nullFID , '12345' )
ans = 0

>> nullFID = fopen('NUL' , 'w' )
Warning: You have chosen a reserved DOS device name for your filename.
Please choose another valid filename
nullFID = -1

>> nullFID = fopen('NUL')
nullFID = -1

R2015a

>> ver
----------------------------------------------------------------------------------------------------
MATLAB Version: 8.5.0.197613 (R2015a)
MATLAB License Number: 1093113
Operating System: Microsoft Windows 7 Professional  Version 6.1 (Build 7601: Service Pack 1)
Java Version: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------
MATLAB                                                Version 8.5        (R2015a)

>> nullFID = fopen( 'NUL:' , 'w' )
nullFID = -1

>> fprintf( nullFID , '12345' )
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

>> nullFID = fopen( 'NUL:' )
nullFID = 8

>> fprintf( nullFID , '12345' )
ans = 0

>> nullFID = fopen('NUL' , 'w' )
Warning: You have chosen a reserved DOS device name for your filename.
Please choose another valid filename. 
nullFID = -1

>> nullFID = fopen('NUL' )
nullFID = -1
Semiotic answered 4/2, 2016 at 22:9 Comment(6)
Why w in one case and not in the other? You should also mention the first is for windows, the second for linux.Caw
@Daniel, I'll expand the answer and include some working scenarios, I'm not sure the role of the 'w', I actually copied the code from several places and got them mixed up along the way. It doesn't look like its needed but it does look like it changes the file ID in some mysterious way.Semiotic
'w' opens the file for writing and discards the existing contents (as opposed to 'a', which appends). I doubt discarding the existing contents of a null device makes sense, so that may be the issue.Traditor
@SamRoberts that's correct about what those parameters are, essentially the open mode, or in Matlab lingo the permission parameter. Permission: String that describes the type of access for the file: read, write, append, or update. Also specifies whether to open files in binary or text mode, but I guess I was loosely trying to word that I did not understand the role of the permission setting in the context of the null device. I'mma remove it since it seems to only work with no permissions always.Semiotic
If you omit the permission it defaults to 'r' meaning read-only - which doesn't make much sense for a null device.Traditor
Now that's an interesting twist!Semiotic
T
3

R2018a

None of the suggestions above appear to work in R2018a anymore:

>> ver matlab
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.4.0.813654 (R2018a)
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 16299)
Java Version: Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB                                                Version 9.4         (R2018a)
>> nullFID = fopen( 'NUL:' , 'w' )
Error using fopen
The file name contains characters that are not contained in the filesystem encoding.
 Certain operations may not work as expected.

>> fprintf( nullFID , '12345' )
Undefined function or variable 'nullFID'.

>> nullFID = fopen( 'NUL:' )
Error using fopen
The file name contains characters that are not contained in the filesystem encoding.
 Certain operations may not work as expected.

>> fprintf( nullFID , '12345' )
Undefined function or variable 'nullFID'.

>> nullFID = fopen('NUL' , 'w' )
Warning: You have chosen a reserved DOS device name for your filename.
Please choose another valid filename. 

nullFID =

    -1

>> nullFID = fopen('NUL' )

nullFID =

    -1
Tread answered 25/3, 2018 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.