MATLAB null conditional operator
8 ビュー (過去 30 日間)
古いコメントを表示
Hi, All
I'm interested in MATLAB null conditional operator or something very similar.
Suppose I have logger in MATLAB class/methods but want to use this class in another small project, where no logger is set up.
In C# I would code would look like
logger?.warn('WARNING');
...
logger?.debug('Three is 3');
and if external code sets logger to null, no calls would be made. So code continues to work just fine, like no logger is present. This is called null conditional operator.
Is there a similar idiom for MATLAB? How it could be done in most simple and elegant way?
Our logger is a singleton wrapping around log4j java class
0 件のコメント
回答 (2 件)
Stephen23
2024 年 1 月 25 日
編集済み: Stephen23
2024 年 1 月 25 日
"How it could be done in most simple and elegant way?"
I assume that the aim is to not change the logging calls within the project code.
One approach would be to define a simple class that accepts any method call and does ... nothing:
logger = MyLogger();
logger.warn('WARNING'); % no error!
logger.debug('Three is 3'); % no error!
The class really is simple and does nothing:
type MyLogger
0 件のコメント
Hassaan
2024 年 1 月 25 日
編集済み: Hassaan
2024 年 1 月 25 日
Example Using 'isempty'
If your logger can be [] (empty) when it's not set:
if ~isempty(logger)
logger.warn('WARNING');
...
logger.debug('Three is 3');
end
Example Using 'isvalid'
If your logger is an object handle that can be invalid when not set:
if isvalid(logger)
logger.warn('WARNING');
...
logger.debug('Three is 3');
end
To make it more elegant and less repetitive, especially if you are doing this check frequently, you could wrap this logic in a method within your class or as a separate function:
function safeLog(logger, logType, message)
if ~isempty(logger) && ismethod(logger, logType)
logger.(logType)(message);
end
end
And use it like:
safeLog(logger, 'warn', 'WARNING');
...
safeLog(logger, 'debug', 'Three is 3');
Making your code cleaner and easier to maintain. It also adds flexibility, as you can easily extend or modify the logging behavior in one place. Remember that MATLAB's object-oriented capabilities are different from those in C#, so some adaptations are necessary.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!