How can I search for all occurrences of built-in MATLAB functions called from multiple .m and .mlx files?

59 views (last 30 days)
I am teaching numerical methods with MATLAB, and the teaching material I have developed over time includes a vast number of MATLAB files (.m and .mlx) stored in a folder and its subfolders on my laptop.
I want to create a table listing all built-in MATLAB functions called in the code across these MATLAB files. Is there a way to search through all MATLAB files in a folder and its subfolders to identify and tabulate the MATLAB functions used? Ideally, I would like the result in a table format like this:
Function Files
----------------------------------------------
plot <filename_a1>, ..., <filename_an>
fprintf <filename_b1>, ..., <filename_bm>
...
----------------------------------------------

Accepted Answer

Sumukh
Sumukh on 27 Nov 2024 at 3:54
It would be helpful to understand what has already been tried to get the result of built-in functions.
While there is no straight-forward way to perform this, you may refer to the following resources to write the code:
  • The dir function can be used to get the list of files in the current folder and all levels of its subfolders. You can refer to the following documentation to know more:
  • Since the built-in functions are used inside the files, the content of each file must be stored and filtered through using regex to identify functions that have the function name with the curly braces. Kindly refer to the following documentation to know more about regex:
  • The “exist function can be used to identify whether the given word in the filtered content is a built-in MATLAB function. The function returns 5 when the given name is a built-in function. You can refer to the following documentation to understand more about the exist function:
  • The content in the files can be looped through and after filtering through and getting the names of the built-in functions, the data can be formatted as required.
I hope this helps with writing the code.
  1 Comment
Henning Søgaard
Henning Søgaard on 27 Nov 2024 at 8:58
Thanks for your advice. Unfortunately, it seems like building the code based on your ideas will take too much time. Thank you anyway.
I was hoping it would be possible to take advantage of MATLAB's ability to parse code, making it feasible to extract a list of functions directly from the interpretation process.
What I have tried so far is listing all .m and .mlx files and using tools like ChatGPT and Microsoft Copilot to generate a list of the functions within these files. This approach works reasonably well, but there are some drawbacks:
  • I have to upload each file individually.
  • There are limits to how many files I can analyse per day.
  • As far as I know, each prompt to a generative AI system requires significant computational resources.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Nov 2024 at 4:19
You might try to get a list of functions by using dir() to find all m-files in all subfolders of your MATLAB installation folder. I'm not sure it will find all built-in functions though. It might find a lot of them though. Then loop over all of your own m-files written by you and open them up with something like fileread or readlines and search the contents of your custom written for each of the function names you found.
You might find the FAQ useful: Process a sequence of files

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!