MATLAB Answers

My matlab software crashes when i run my code without giving any error log.

84 views (last 30 days)
Jai  Kumar
Jai Kumar on 24 Sep 2018
Commented: Walter Roberson on 24 Sep 2018
I am trying to run this program. But when it reaches second for loop it crashes without any output. If I run it for one loop it is fine. Memory may be the issue here because of recursion. How can I manage the memory? Program files are attached.
  1 Comment
Walter Roberson
Walter Roberson on 24 Sep 2018
There is no point trying to debug code that uses eval() that extensively (unless perhaps you are getting danger pay to deal with the toxic waste code.) Just delete that function and rewrite it from scratch based upon the documented interface definition for what it is to do.

Sign in to comment.

Answers (1)

OCDER
OCDER on 24 Sep 2018
Before fixing memory issues, without getting an "Memory Error" message that would suggest this is the issue, perhaps look into rewriting your code to avoid scripts, clear, global and eval. These are much larger issues than your crashing error - which should resolve itself when you rework your code using best practices.
global: it's hard to figure out what operation is modifying your variables, and in what order. Use regular variables and pass them into your functions as inputs.
script: does not compartmentalize your tasks, which means you'll end up using clear to clear up memory, which is very slow and makes your code much more difficult to maintain as you must search and clear variables you don't need. Also, functions will generate an error if something goes wrong, so you can debug just that function.
clear: takes up a lot of time and it's hard to keep track of temporary variables. With functions, temporary variables are automatically cleared when function completes, saving only the outputs.
eval: worst function to use, as matlab cannot pre-check your code for potential error, as in anything can go and crash your computer. You need to use the appropriate function name instead
Read these blogs for starters, and begin reworking your code. Also, use debugging tools. For crashes, consider running your code line-by-line until it crashes (hopefully, your reworked code won't crash but will return an error message instead).
  4 Comments
OCDER
OCDER on 24 Sep 2018
The alternative to using dynamically renamed variable is to use dynamically named fields such as V.A1, V.A2, V.A3, etc. You can use fieldnames to get a1-aN field names, and then just go through the structure.
example:
for j = 1:4
eval(['A' num2str(j) '= j']) %not good
V.(['A' num2str(j)]) = j %okay, but you have to use V.A1, V.A2, ... Another way is to not use struct, but cells like:
C{j} = j %but you do lose the field name, if that's important for code readability
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by