Code get stuck with nested loop and computer crashes after (2024)

5visualizaciones (últimos 30días)

Mostrar comentarios más antiguos

Victor Gimenez el 19 de Abr. de 2022

  • Enlazar

    Enlace directo a esta pregunta

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after

  • Enlazar

    Enlace directo a esta pregunta

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after

Editada: Victor Gimenez el 20 de Abr. de 2022

Respuesta aceptada: Image Analyst

I am having an issue to run this block of code:

clear all

set(0,'DefaultFigureVisible','off');

a_data = cell(1,12560);

fft_a_data = cell(1,12560);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%Code lines here before%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

count = 1;

for p2 = 1:a_trials

for p1 = 1:16

figHandle = figure;

topoplot(a_data(p1,:,p2),EEG.chanlocs,'style','map');

[X, Map] = frame2im(getframe(figHandle));

a_data{count} = X;

%FFT

Y = fft(a_data(p1,:,p2));

P2 = abs(Y/L);

P1 = P2(1:L/2+1);

P1(2:end-1) = 2*P1(2:end-1);

figHandle = figure;

topoplot(P1(fft_index),EEG.chanlocs,'style','map');

[X_fft, Map_fft] = frame2im(getframe(figHandle));

fft_a_data{count} = X_fft;

count = count + 1;

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%Code lines here after%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

set(0,'DefaultFigureVisible','on');

At the beginning the computer looks like running it but after it stops and whole system crashes, how should I handle this issue? When I consider a range for p2=1:5 and p1=1:5 it is running normally but to this range p2=1:785 I can't able to perform it. The topoplot belongs to EEGLab toolbox.

0 comentarios

Mostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Iniciar sesión para responder a esta pregunta.

Respuesta aceptada

Image Analyst el 20 de Abr. de 2022

  • Enlazar

    Enlace directo a esta respuesta

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#answer_946300

Abrir en MATLAB Online

Why are you plotting those things anyway? Just to look at them? How about if you just opened a figure before the loops, and then looked at the pair of plots inside the inner loop? You're overwriting X, Map, X_fft, and Map_fft on each inner loop iteration anyway. Why? Were you planning on making a movie out of all of them?

clear all

% set(0,'DefaultFigureVisible','off');

a_trials = 2; % Whatever...

a_data = cell(1,12560);

fft_a_data = cell(1,12560);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%Code lines here before%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

count = 1;

abort = false;

% Create new figure and maximize it.

figHandle = figure;

figHandle.WindowState = 'maximized';

for p2 = 1:a_trials

for p1 = 1:16

% Make first plot in upper half of the figure.

h1 = subplot(2, 1, 1);

cla;

topoplot(a_data(p1,:,p2),EEG.chanlocs,'style','map');

[X, Map] = frame2im(getframe(h1));

a_data{count} = X;

%FFT

Y = fft(a_data(p1,:,p2));

P2 = abs(Y/L);

P1 = P2(1:L/2+1);

P1(2:end-1) = 2*P1(2:end-1);

% Make second plot in lower half of the figure.

h2 = subplot(2, 1, 1);

cla;

topoplot(P1(fft_index),EEG.chanlocs,'style','map');

[X_fft, Map_fft] = frame2im(getframe(h2));

fft_a_data{count} = X_fft;

count = count + 1;

promptMessage = sprintf('Look at your plots for p1=%d and p2=%d.\nDo you want to Continue processing,\nor Quit processing?', p1, p2);

titleBarCaption = 'Continue?';

buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');

if contains(buttonText, 'Quit', 'IgnoreCase', true)

abort = true;

break; % or break or continue.

end

end

if abort

break;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%Code lines here after%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% set(0,'DefaultFigureVisible','on');

1 comentario

Mostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Victor Gimenez el 20 de Abr. de 2022

Enlace directo a este comentario

https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2112245

  • Enlazar

    Enlace directo a este comentario

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2112245

Editada: Victor Gimenez el 20 de Abr. de 2022

Hey there @Image Analyst, thank you for this "shook"!!! In fact my knowledge is very basic in MATLAB's image processing functions and techniques, this method call and attribution: figHandle = figure; I followed it through the topoplot EEGLab function but I stayed confused the difference between execute topoplot directly that I realized that I can open the figure and call figure and after topoplot, and I was unfamiliar with the WindowState attribute also, big thanks for your help again you saved me!! I am gonna study more this implementation and follow this idea on the next scripts!!

Iniciar sesión para comentar.

Más respuestas (1)

Jan el 19 de Abr. de 2022

  • Enlazar

    Enlace directo a esta respuesta

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#answer_946105

  • Enlazar

    Enlace directo a esta respuesta

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#answer_946105

What is a_trials?

It looks like you want to run 12560 iteration. You open two figures in each iteration, so there is a total of 25120 open figures. Although they are invisible, they need a lot of memory. The crash means, that the memory is exhausted.

Either close the figures, if you do not need them anymore. Or reuse the existing figures, which would be much moire efficient. See e.g. clf.

2 comentarios

Mostrar NingunoOcultar Ninguno

Victor Gimenez el 19 de Abr. de 2022

Enlace directo a este comentario

https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2111525

  • Enlazar

    Enlace directo a este comentario

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2111525

This loop for: p1 = 1:16 represents the corresponding EEG electrodes that are being used and p2 = 1:a_trials the epochs/trials in each electrode that are 785 and there is another nested loop similar this one next this until 780 representing epochs/events from other group, yes there are 12560 iterations going on and because it that I performed set(0,'DefaultFigureVisible','off'); before whole routine, so I am gonna put this clf to verify!! Thanks for the tip at the moment Jan!

Victor Gimenez el 20 de Abr. de 2022

Enlace directo a este comentario

https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2111935

  • Enlazar

    Enlace directo a este comentario

    https://la.mathworks.com/matlabcentral/answers/1699965-code-get-stuck-with-nested-loop-and-computer-crashes-after#comment_2111935

Editada: Victor Gimenez el 20 de Abr. de 2022

Hey Jan, I could able to solve it 1/3 I'd say, when the count variable was around 3341 my whole environment crashed again... but it was more far than before

Iniciar sesión para comentar.

Iniciar sesión para responder a esta pregunta.

Ver también

Categorías

RadarPhased Array System ToolboxWaveform Design and Signal SynthesisMatched Filter and Ambiguity Function

Más información sobre Matched Filter and Ambiguity Function en Help Center y File Exchange.

Etiquetas

  • loops
  • for loop
  • signal
  • vectorization
  • cell arrays

Productos

  • MATLAB

Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Se ha producido un error

No se puede completar la acción debido a los cambios realizados en la página. Vuelva a cargar la página para ver el estado actualizado.


Translated by Code get stuck with nested loop and computer crashes after (7)

Code get stuck with nested loop and computer crashes after (8)

Seleccione un país/idioma

Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .

También puede seleccionar uno de estos países/idiomas:

América

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia-Pacífico

Comuníquese con su oficina local

Code get stuck with nested loop and computer crashes after (2024)

FAQs

Can a while loop crash your computer? ›

If you don't have a super CPU you will immediately crash most browsers with that loop and anything that doesn't crash won't work and will take up most of CPU. While the code is stuck in that loop JS can't do anything else like run the events, so game can never get set to true, and the loop can never end.

Why do infinite loops crash? ›

Infinite loops occur when loops have no exit condition (no way to stop) so when the program is run it loops forever with no break, causing the browser to crash. This happens most often with while loops, but any kind of loop can become infinite.

Can an infinite loop break your computer? ›

In older operating systems with cooperative multitasking, infinite loops normally caused the entire system to become unresponsive. With the now-prevalent preemptive multitasking model, infinite loops usually cause the program to consume all available processor time, but can usually be terminated by a user.

Can CPU overload cause crash? ›

However, abnormally high CPU usage can cause the computer to stutter, become unresponsive, or crash. If your computer seems to overwork its CPU even when high-intensity applications are closed, it may indicate a deeper problem.

Are infinite loops bad for CPU? ›

Infinite loops can cause various problems, such as freezing your program, consuming your CPU resources, or producing unwanted outputs.

Why should programmers avoid using infinite loops? ›

Infinite loops are loops for which the condition will always be true. The problem is that you can't determine whether that will happen for every possible loop. It's a case of the Halting Problem. There's no universal way to determine that a program (or in this case, a loop) will terminate.

Could using a forever loop cause any problems with code? ›

Infinite loops are a pain. In general, running an infinite loop can eat up your computer's resources and then freeze your IDE—and sometimes even your whole machine. Inside the CodeSignal IDE, letting infinite loops run unchecked would eventually slow down or crash the UI.

Is it bad to use while loops? ›

This is sometimes invaluable, but comes with a danger: sometimes your while loop will never stop! This is called an “infinite loop”. If the condition stays true, the while loop will keep on executing the block — until the end of the universe, if need be. This is usually not what you want!

What is the danger of the while loop? ›

The code inside of the while loop does not modify the variable counter , so the halting condition will never evaluate to false. This loop will run forever (or until the program is stopped). Infinite loops cause headaches for programmers and users alike because the program itself can never move forward.

What happens if you break a while loop? ›

The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement. break is not defined outside a for or while loop.

Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5857

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.