我正在尝试找到一种简单的方法来将日志记录打开和关闭,就像Python中的here示例一样。他们的解决方案是使用需要记录的有效文件名,否则使用null device 'dev/null'
。他们正在使用重定向,但我希望使用fopen
。
是否有某种方法可以执行以下操作:如果重要的话,哪个词适用于Unix或Windows系统。
nullFID = fopen('/dev/nul', 'w')
我正在改变的脚本可以通过在某些突发场景中详细记录来获益,在这些场景中我们希望为少量文件提取精细的详细信息,但是对于脚本通常目标的批处理而言,这会非常大
答案 0 :(得分:3)
看起来可以这样做,有人在MathWorks上使用类似术语“空文件”发布了答案。不确定这是否是有效的术语,但可以缩短“空设备文件”。
引用空设备的方法是依赖于OS ...
nullFID = fopen( 'NUL:' ); % Windows
nullFID = fopen('/dev/null'); % UNIX
此外,该参考文献似乎与Matlab版本不同。请考虑以下脚本
ver
nullFID = fopen( 'NUL:' , 'w' )
fprintf( nullFID , '12345' )
nullFID = fopen( 'NUL:' )
fprintf( nullFID , '12345' )
nullFID = fopen('NUL' , 'w' )
nullFID = fopen('NUL' )
以下输出是从两个版本的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
答案 1 :(得分:1)
以上建议似乎都不再适用于R2018a:
>> 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