Assistance Needed - Need to speed up a script for GUI (Main Problem - Extensive use of 'set')
1 view (last 30 days)
Show older comments
Hello Everyone//
I need your help to speed up my script (posted in the following comment). I use it to simulate different stock prices and their related functions. The main algorithm can loop up to 25+ times depending on the randoms generated.
I have used a for loop in the callback function of a button, which will call the main algorithm function according to the number of times specified. The script is working very fine with thousands of simulations but the only problem is the time it is taking, and I believe this is a result of an excessive use of "setvalues" of edit text fields in the GUI.
Please find the following problems I am facing with the current script, I think if they are solved, the script will be much faster. Any suggestion is very considerable as it might help me alot.
1- At the beginning of each simulation I run a "reset" function, that resets the values of the different edit text fields to the values that were initially chosen by the user. The problem is that I am using the "set" function which, as I understand, takes time. Is there a way to overcome this problem?
2- As a continuation to the above point, I use "set value" for an edit text field in the GUI twice in the main algorithm for a variable that changes over the script. For example, at the beginning it can be 100, then it becomes 102 then 103, when there is a need for a loop I need the beginning value to be the last reached one = 103. I am doing this as stated by the set value function, but again this is taking time. Anyway to overcome this?
3- This is the most important problem I think: As you might notice in the script, I use the following 2 times in the for loop, and another 2 times in its nested if:
summ=str2double(get(handles.Knock,'String'));
y=get(handles.Knock,'userdata');
y=[y summ];
set(handles.knock,'userdata',y);
By this, I get the vector of different generated values in each of the for loops. I knew that I need pre-allocating this as it might be faster. I tried defining the size of y=zeros(1,1000) for example but this has no effect. Any idea. Moreover, is there any other way to overcome the "set" function again in this code.
Thanks in advance guys// Ali
Answers (2)
Azzi Abdelmalek
on 11 Jan 2013
Edited: Azzi Abdelmalek
on 11 Jan 2013
You will need to use a counter, Try also to use guidata
3 Comments
Azzi Abdelmalek
on 12 Jan 2013
Edited: Azzi Abdelmalek
on 12 Jan 2013
It's not clear for me. What do you mean: when it loops in the for loop
If you use a counter you have to store it, for example by using guidata or userdata
counter=counter+1
handles.count=counter
guidata(hObject, handles);
To get your counter
counter=handles.count
Jan
on 12 Jan 2013
It seems like you have mixed the actual calculations and the GUI interface. While calculations like a*b or a+b take 1e-9 to 1e-6 seconds (just a rough estimation), updating a GUI takes 0.01 to 0.1 seconds. On the other hand human are not able to read a GUI with such a frequency, such that everything below 1Hz should not be displayed in a GUI (except for animations/movies, but this is a completely different story). In general data, computations and the user interface should be separated carefully.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!