3

I am new to matlab:

I was working in python, I generally use logging a lot to keep track of the code. In python, we can use below

import logging
logger = logging.getlogger()
logger.setLevel(logging.INFO)
handler = logging.FileHandler("log.txt",mode=w+)
handler.setlevel(loggin.INFO)
logger.addHandler(handler)

#And any where in the code use it as

logger.info("testing")

How can we do something similar in matlab

2
  • MATLAB has a wonderful debugger, you should look into that rather than logging. Plus, you can use warning to output comments for the user that can be turned off. Commented Nov 11, 2020 at 19:03
  • debugger is good, but if we have logging, its like you can just turn on and off and on a sepated file we can see what happened. good to track Commented Nov 12, 2020 at 3:29

1 Answer 1

7

There's no built-in logging feature in Matlab. So you need to use third-party or undocumented tools.

Matlab ships the Java logging libraries SLF4J and log4j with its bundled JVM. You can make use of them for your logging. There's a Matlab SLF4M library that provides a layer of convenient Matlab bindings for SLF4J/log4j. (Full disclosure: I wrote SLF4M.)

There are also some native M-code Matlab logging libraries available on the MathWorks File Exchange:

Sign up to request clarification or add additional context in comments.

2 Comments

github.com/janklab/SLF4M looks very much what i wanted. Can we use string concatenation like logging.debug("a :: "+string(a)+" :: b :: "+string(b)) instead of sprintf format like logging.debug("a :: %s :: b :: %s,(a,b))
Yes. String concatenation works in any context where a plain string works. (And if SLF4M chokes on the fact that it's a string instead of a char, I'll put in a fix for it quickly.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.