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

21 visualizaciones (últimos 30 días)
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 comentario
Walter Roberson
Walter Roberson el 24 de Sept. de 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.

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 24 de Sept. de 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 comentarios
OCDER
OCDER el 24 de Sept. de 2018
Here's something to get you started. I don't have time to fix everything, but you should be able to figure out how to fix things up according to the guidelines for best practices.
function my_data = jk_encoder(varargin)
set(0,'RecursionLimit',2000); %perhaps build a recursion funtion that will stop recursions it self
%Preallocate this
% key = {};
% count = 0;
% my_data = [];
% value = {};
y = rgb2gray(imread('a.png'));
my_data = zeros(size(y, 1), 8);
for i = 1:size(y, 1) %1:1:length(y) %use size(y, Dim) instead, as length(y) takes the longer one of row or height
for j = 1:size(y, 2) %length(y)
z = fliplr(de2bi(y(i,j),8));
%Why do you need to save this into a-h variables?
my_data(i, 1:8) = z(1:8);
%or, save directly to your output structure, M1
M1.(Fields{i}) = ... But, you'll need to setup M1 before this loop then.
% a(i,j) = z(1);%MSB
% b(i,j) = z(2);
% c(i,j) = z(3);
% d(i,j) = z(4);
% e(i,j) = z(5);
% f(i,j) = z(6);
% g(i,j) = z(7);
% h(i,j) = z(8); %LSB
end
end
%clear y z
key1 = {'MSB','V1','V2','V3','V4','V5','V6','LSB'}; %You could also just us a structure... but careful with the naming.
val = %{a,b,c,d,e,f,g,h}; %Fix this so that M1.MSB = my_data(1, :), M1.V1 = my_data(2, :), etc. You can use dynamic field names. Look it upt.
%M1 = containers.Map(key1,val);
Temp = [key1; val];
M1 = struct(Temp{:});
for k = 1:length(key1)
jk_en_test(M1.(key1{k})); %you could use dynamic field names
end
% for i = 1:2
% key = {}; %These would be auto cleared
% value = {};
% count = 0;
% my_data = [];
% jk_en_test(M1(char(key1(1)))); %Why input this like this?
% fprintf('Output of %s plane =\n %d',char(key1(1)),my_data);
% clear M1(char(key1(1))) key value count M
% end
% load('plane.mat')
%jk_en_test(M1(char(key1(1))));
% out = [out 0 0 0];
% f = []
% for l = 1:ceil(length(out)/4)
% f(i)=bi2de(fliplr(out(((i-1)*4+1):i*4)));
% end
toc
OCDER
OCDER el 24 de Sept. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by