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

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 个评论

There is no such thing as an "if loop". "if" executes the body exactly 0 (false) or 1 (true) time, never more. "loops" may execute their loop bodies multiple times.
"if" is the "if statement", not "if loop".
Many Thanks Walter.
Well noted; then, I would now edit the title as follows:
Value changes while the function calls itself.
By the way, do you know how to get into solving the issue without updating the GUI which is time consuming.
Thanks & All Best// Ali
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

请先登录,再进行评论。

回答(1 个)

Why don't you use a while loop? From a short inspection, you change "if" to "while" and it does what you want.

5 个评论

Thanks a lot Jason,
Actually I am not having problems with the use of "if", but with the process of updating the value of StockPrice when the function calls itself again. Even with the use of "while", I would get into the same problem. I tried handles.StockPrice but it doesn't work of course.
If there was no initial value, there wouldn't have been a problem I guess. Furthermore, I think that I can overcome my problem by using couple of more if statements at the beginning of the script but it doesn't seem right and it will consume more time to run the script.
Just to add that in my script, there are much more if statements and couple of for loops, but the code I provided first is just a very short summary that serves the purpose.
Thanks again Jason and all best// Ali
When I get to this stage any any programming language, I step away from the keyboard and start drawing out the problem on paper or a whiteboard. It seems that you need to think about dividing your problem into control flow, algorithm and display to make things easier.
It's pretty much the "model-view-controller" approach: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Hello again Jason,
Many thanks for your reply, but does that mean that there is no solution to the current scenario? I was thinking I could use something similar to the set-value criteria but inside the script (function) itself instead of text field inside the GUI; I thought this would be achievable - I have searched alot to say, but still unable to find a way around!
THanks again and all best// Ali
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.
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

提问:

AND
2013-1-22

Community Treasure Hunt

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

Start Hunting!

Translated by