Why does my GUI take forever every time I try to plot using values retrieved from edit boxes?

1 vue (au cours des 30 derniers jours)
I currently have a script that is meant to plot a function and it works perfectly well when it is run on its own and values for certain variables are hard coded in. I want to make a GUI to run this program so the user can define certain characteristics of the function by giving it some of the variables values by using edit boxes. When I run the program, every other aspect of the GUI works perfectly until I try to plot. At this point the GUI takes an incredibly long time to plot the values.
I have tested the plot function in the GUI by hard coding in the variable values and this works very quickly (the plot shows up almost instantly), it is only when I use a get function to define these variables that I have the problem. I use get functions a lot in the rest of the GUI and they work fine/quickly, so I do not know why this is the causing problems when I try to use it in conjunction with the plot function. There are a considerable number of data points (about 3500000) and I am trying to let the user define 5 variables which might be why it is taking so long, but again, when the variable are hard coded in, the GUI is able to graph all 3500000 data points almost instantly.
Any ideas on how to make this code run faster?
  2 commentaires
Adam
Adam le 26 Juil 2017
You need to show us the code in question, otherwise it would be pure guessing.
Kelly Babitz
Kelly Babitz le 26 Juil 2017
This is run as a callback to a pushbutton.
Thanks in advance for your help!
handles = guihandles(figure);
timeIncrement = .0001;
times = (0:timeIncrement:350);
peakTimeA = str2double(get(handles.peakTimeImpulse1,'String'));
widthA = str2double(get(handles.widthImpulse1,'String'));
concLeftVentA = normpdf(times,peakTimeA,widthA)+.0005;
secondImpulseCheck = get(handles.secondImpulseCB,'Value');
if secondImpulseCheck
peakTimeB = str2double(get(handles.peakTimeImpulse2,'String'));
widthB = str2double(get(handles.widthImpulse2,'String'));
heightMultiple = str2double(get(handles.heightMultImpulse2,'String'));
concLeftVentB = heightMultiple*normpdf(times,peakTimeB,widthB)+.0005;
else %if its not checked, then there is no part b to the function
concLeftVentB = 0;
end
concLeftVent = concLeftVentA + concLeftVentB;
concMyo = zeros(1,length(times));
for t = 2:length(times)
if t < 150/timeIncrement
k1 = str2double(get(handles.k1Impulse1,'String'));
k2 = str2double(get(handles.k2Impulse1,'String'));
else
k1 = str2double(get(handles.k1Impulse2,'String'));
k2 = str2double(get(handles.k2Impulse2,'String'));
end
added = concLeftVent(t)*timeIncrement*k1;
out = k2.*concMyo(t-1);
concMyo(t) = concMyo(t-1) + added - out;
end
plot(times,concLeftVent,times,concMyo);

Connectez-vous pour commenter.

Réponses (1)

Adam
Adam le 26 Juil 2017
Modifié(e) : Adam le 26 Juil 2017
I would suggest using
doc profile
on this to see where the main time is being spent.
My guess would be this though:
if t < 150/timeIncrement
k1 = str2double(get(handles.k1Impulse1,'String'));
k2 = str2double(get(handles.k2Impulse1,'String'));
else
k1 = str2double(get(handles.k1Impulse2,'String'));
k2 = str2double(get(handles.k2Impulse2,'String'));
end
You are doing that in a very long loop. You should query the edit boxes outside the loop. Calling str2double 14 million times will take a while!
Just get the two alternatives from the UI before the loop and then use whichever is needed.
You may be able to vectorise the whole loop, but certainly moving the UI access outside will be a big improvement.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by