Assistance Needed - Need to speed up a script for GUI (Main Problem - Extensive use of 'set')

1 view (last 30 days)
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
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
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
AND
AND on 12 Jan 2013
Thanks alot Azzi,
before anything else - do you think the set functions are the ones causing the slow process? Do you also think the get function (str2double(get(handles.Knock,'String'))) might slow the process too?
For the particular code below, I have found the way, I just use k(i) = y instead of the set function
summ=str2double(get(handles.Knock,'String'));
y=get(handles.Knock,'userdata');
y=[y summ];
k(i)=y;
But this doesn't seem to speed up the process, as I still have other setvalue functions which can be summarized in the following example:
Button_CallBack
for i=1:10
set(handles.StockP,'String',(100))
Call FunctionOne
end
FunctionOne
1- Some calculations based on StockP=100 (I retreive using str2double)
2- Other calculations = x
3- If x>0, calculate new StockP = 102, set(handles.StockP,'String',(102)) and go to step 1. Now the get function in step 1 will get a price of 102 and this is exactly how it should be.
4- else x=<0, calculate new StockP = 105 the remaining same like in step 3.
I want the replicate the code above without using the set function.
What do you think?
Best// Ali

Sign in to comment.


Jan
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.
  1 Comment
AND
AND on 20 Jan 2013
Hi Jan, Many thanks for your answer and you have exactly pointed out the problem that is causing this slow work.
It is somewhat weird that I didn't receive an email with your answer!
I apologize for the delay in response.
You are right indeed, I have mixed the calculations with the Interface, as that was the only way to apply the algorithm.
For some calculations, I can omit updating the GUI easily but for others I am still using that method.
For instance, do you think there is a way other than updating the GUI using set value for the following:
Button_CallBack
for i=1:10
ResetFunction %This is a function that resets the values to the initial ones that were choosen by the user. For instance one of them could be:set(handles.StockP,'String',(100)), and then the main script will work this way%
Call FunctionOne
end
FunctionOne
1- Some calculations based on StockP=100 (I retreive using str2double)
2- Other calculations = x
3- If x>0, calculate new StockP = 102, set(handles.StockP,'String',(102)) and go to step 1. Now the get function in step 1 will get a price of 102 and this is exactly how it should be.
4- else x=<0, calculate new StockP = 105 the remaining same like in step 3.
end
All what I need is to find a way to always update the value of StockP in FunctionOne without updating the actual GUI like I am doing now, and then find a similar way to apply instead of the ResetFunction.
I have tried handles.StockPrice = ..., but it doesn't seem to work.
Thanks again Jan & all best// Ali

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!