Value Changes in a script (While the function calls itself)

4 visualizzazioni (ultimi 30 giorni)
Hello Everyone,
I have recently asked a question, but I think that the main idea was lost; so I am going to summarize it hoping that I will get the assistance required.
I have a script that runs from a GUI button. I am getting the results I want at the moment by updating the GUI in every run using (set-get value functions of an edit text field). Thus, I am mixing the calculations with updating the GUI. The problem is that the process is slow and I need to speed it up. I understand that if I get rid of the set-value function (updating the GUI), the process will be much faster. So, the question becomes how to update the value of a variable inside a loop; or in other words, how to achieve this (afterwards, you will find how I am currently achieving what I want):
FunctionOne
InitialPrice=100;
TargetPrice=105;
Time=20; %As you note in the script, this is the maximum number of loops
1- StockPrice= InitialPrice*(1+randn) %just for simplicity
3- If Time > 0
If StockPrice>TargetPrice
update the GUI
else
set Time = 20-1;
FunctionOne %Now I call the function again and Here is the problem: now I want to start over, but the StockPrice calculations will be based on the last calculated price, so if the calculated StockPrice in step 1 = 102, now the new StockPrice=102*(1+randn) and not 100*(1+randn)
end
end
end
Here is how I am getting the results currently by updating the GUI:
FunctionOne
InitialPrice=str2double(get(handles.StockP,'String'));
TargetPrice=105;
T=str2double(get(handles.Time,'String'));
1- StockPrice= InitialPrice*(1+randn) %just for simplicity
3- If Time > 0
If StockPrice>TargetPrice
update the GUI %end
else
set(handles.StockP,(StockPrice))
set(handles.Time,'String',(T-1))
FunctionOne
end
end
end
Any Help is really appreciated.
Thanks// Ali
  4 Commenti
AND
AND il 22 Gen 2013
Modificato: AND il 22 Gen 2013
Hello Walter again,
Just to add that I used the term "if-loops", because I am calling the function from itself again when the statement is false.
Best// Ali

Accedi per commentare.

Risposte (1)

Jason Ross
Jason Ross il 22 Gen 2013
Why don't you use a while loop? From a short inspection, you change "if" to "while" and it does what you want.
  5 Commenti
Jason Ross
Jason Ross il 23 Gen 2013
Modificato: Jason Ross il 23 Gen 2013
I can't say, since you say you have simplified things down for this example.
I'm telling you that from my programming experience that when things start to get ugly and confusing, it is often worth the time to step away from the code and really think out what you are doing, and try to simplify things to small blocks of code that do one thing really well. I like a whiteboard or sheet of paper, but some people like sticky notes on a wall or various diagramming options or programs to figure out what has to happen when, and what the desired outputs should be.
It seems to me that you are trying to intermix the solution of at least two problems (one is the actual problem you are trying to solve, the other is displaying that on a GUI). The most important problem you have is getting the algorithm right. So I'd worry about that first, and make sure it's giving you the correct results. I'm suspecting that some calculation that's going on in FunctionOne is the really important thing, and you need to be able to call that when you need to. The second thing that's going on is some comparison of TargetPrice and StockPrice and the third is time based. You are also mixing in the creation of initial values in the function, as well.
The second big problem is that you need to update the GUI only when appropriate. Once you've figured out the algorithm and are getting the desired results and behavior, move onto the GUI. I'm guessing the result there will be an update to the GUI at the end of each time iteration.
AND
AND il 24 Gen 2013
Thank you very much Jason for the comprehensive information. I shall follow your steps to see if I can come up with an alternative way to have this done.
I again thank you very much and wish you all best// Ali

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by