live plotting inside for loop in Matlab GUI
3 ビュー (過去 30 日間)
古いコメントを表示
Hi guys,
In my GUI, among many other UI controls, there are a pushbutton and an edit text. The pushbutton calculates various nonlinear optical effects, and the edittext changes one of the parameters (let's say "a") and activates the pushbutton again via the following line: myfilename('pushbutton_Callback',handles.pushbutton,[],handles);
Now what I want is to change "a" inside the pushbutton callback with a for loop, and to plot the results in live regime. I should note that I am able to individually plot the result data points after every loop, but I want to be able to connect these data points.
Here is my edittext callback that was supposed to work according to my understanding:
function a_Callback(hObject, eventdata, handles)
a = str2num(get(handles.a, 'String'));
step_a = str2num(get(handles.step_a, 'String'));
kk = 10;
for k = 1:kk
a = a + step_a;
set(handles.a, 'String', num2str(a));
myfilename('pushbutton_Callback',handles.pushbutton,[],handles);
x = handles.x;
y = handles.y;
x1(k) = x;
y1(k) = y;
axes(handles.axes1);
hold on;
plot(x1(1:k),y1(1:k));
% plot(x1(1:k),y1(1:k),'.');
hold on
drawnow
end
guidata(hObject, handles);
It seems that x and y are not updated after activating the pushbutton inside the loop. I checked this by plotting with dots (commented inside the code): it plots just the first data point and that's it.
Any help would be appreciated. Thanks!
0 件のコメント
採用された回答
Walter Roberson
2017 年 4 月 7 日
If handles.x and handles.y are numeric and being updated outside of the function, then you need to fetch their current values. Replace
x = handles.x;
y = handles.y;
with
newhandles = guidata(hObject);
x = newhandles.x;
y = newhandles.y;
Remember, the handles structure is a copy of whatever the "master" version of the handles structure was at the time the routine was invoked, so changes to the master version are not reflected "live". If you have reason to think something else changed the master copy and you care, then you have to fetch the master again.
3 件のコメント
Image Analyst
2017 年 4 月 7 日
I'm surprised that fixed it. What do you have going on in the background that is changing those? I asked that in my solution but you haven't given an answer. Do you have a timer or something running? Or a parallel process going (if you have the Parallel Computing Toolbox)? What is changing those? If that process and this one are not in sync, it seems kind of risky, like you could have a potential (but not definite) racing or timing problem.
Walter Roberson
2017 年 4 月 7 日
They have an edit box with a callback that is changing the values. They have a drawnow() in the pushbutton callback, so they are giving an opportunity for the edit callback to fire.
その他の回答 (1 件)
Image Analyst
2017 年 4 月 7 日
Then it appears that handles.x and handles.y are the same value each time. Why do you think that they would change? You're not doing anything that I can see to change them on each iteration. So they just stay the same.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!