本文介绍了如何从GUI回调函数中断函数,而不向函数添加中断检查? (MATLAB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以,我有一个GUI,基本上允许用户迭代地处理数据。因此,存在开始/停止按钮和显示数据的当前状态的显示器。当您单击开始按钮时,回调函数调用如下所示的数据处理函数: function result = process_data(data) result = 0; for big_loop = big_start:big_end for small_loop = small_start:small_end result = result + data; %现实中只有一些符号矩阵运算 end end 我的问题是如何实现停止按钮的回调,以便在当前小循环迭代后从process_data返回。 现在我通过将process_data代码修改为如下: function result = process_data_new(data) result = 0; for big_loop = big_start:big_end for small_loop = small_start:small_end result = result + data; %在现实中只是一些鸽子矩阵ops %新代码开始----------------- 如果interrupt_flag return; end %新代码结束-------------------- end end 这是否可能?我想象它会涉及到所有的循环变量的可观察的属性,然后等待small_loop改变,但我不能弄清楚如何去实现回调的细节。解决方案可能是有帮助的: 有一个按钮'stop',在回调中,在较小的循环中(即,在循环中忙的其他回调),它将检查该标志在其(小)的顶部(或底部)处的真(true)。 如果标志设置为true,则立即使其为假,并终止整个循环并返回。 (在循环中添加一个检查,以检查是否设置了此标志) 因此,用户在敲击STOP后最多只能等待一个小的迭代完成if 这个片段代码来自我有一个例子 小循环示例 function status = output(t,x,〜,data)每个步骤后由ode45调用的%。绘制当前%模拟的摆动位置 userData = get(data.handles.figure1,'UserData'); if userData.stop status = true; g_status = true; else status = false; ....做实际工作 end end run按钮回调 % - 在run_btn按下按钮时执行。 函数run_btn_Callback(hObject,eventdata,handles)%h run_btn的对象句柄(参见GCBO)%eventdata保留 - 在未来版本的MATLAB中定义%带有句柄和用户数据(参见GUIDATA) [data,status] = parse_input(handles); 如果不是(status) return; end ..... userData.stop = false; set(handles.configure1,'UserData',userData); ... 停止按钮回呼 $ b b %---在reset_tag按下按钮时执行。 function reset_tag_Callback(hObject,eventdata,handles)%h reset_tag的对象句柄(参见GCBO)%eventdata保留 - 在未来版本的MATLAB中定义%带有句柄和用户数据(参见GUIDATA) data = get(handles.figure1,'UserData'); data.stop = true; set(handles.figure1,'UserData',data); ..... So, I've a GUI that basically allows the user to iteratively process data. Thus, there is a start/stop button and a display that shows the current state of the data. When you click the start button, the callback function calls the data processing function shown below:function result = process_data(data) result = 0; for big_loop=big_start:big_end for small_loop=small_start:small_end result = result+data; %in reality just some fancier matrix ops end endMy problem is how to implement the stop button's callback so that it returns from process_data after the current iteration of the small loop.Right now I do this by modifying the process_data code to be as follows: function result = process_data_new(data) result = 0; for big_loop=big_start:big_end for small_loop=small_start:small_end result = result+data; %in reality just some fancier matrix ops%new code start ----------------- if interrupt_flag return; end%new code end -------------------- end endHowever, I really want the interruption to be handled from the GUI's end so my code isn't littered with interrupt checks (My real code would involve this kind of thing very often, and the user would sometimes need to change the process_data function).Is this possible? I imagine it would involve making all of the looping variables 'observable' properties, and then waiting for small_loop to change, but I can't figure out details of how to go about implementing the callback. 解决方案 may be this helps:have a button 'stop', in its callback, set a flag to true.in the smaller loop (i.e. the other callback which busy in a loop), it will check this flag at the top (or bottom) of its (small) loop.If the flag is set true, it makes it false right away and terminate the overall loop and returns. (add a check in your loop to check if this flag is set)So, the most the user has to wait after hitting STOP is for one smaller iteration to complete if the hit happened just after the last check.callbacks interrupt each others, so the above works.The flag is set in userData buffer and read from there by the other callback each time.This snippets code from one example I havethe small loop example function status= output(t,x,~,data) %called by ode45 after each step. Plot the current %pendulum position for simulation userData = get(data.handles.figure1, 'UserData'); if userData.stop status=true; g_status =true; else status = false; .... do real work end endthe 'run' button callback% --- Executes on button press in run_btn.function run_btn_Callback(hObject, eventdata, handles)% hObject handle to run_btn (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)[data,status] = parse_input(handles);if not(status) return;end.....userData.stop = false;set(handles.figure1, 'UserData',userData);...stop button callback% --- Executes on button press in reset_tag.function reset_tag_Callback(hObject, eventdata, handles)% hObject handle to reset_tag (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)data = get(handles.figure1, 'UserData');data.stop = true;set(handles.figure1, 'UserData',data);..... 这篇关于如何从GUI回调函数中断函数,而不向函数添加中断检查? (MATLAB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 16:19