Matlab使循环同时运行
不幸的是,我有两个循环。这就是为什么我的代码会运行第一个循环,只有当它完成时,才会运行第二个循环 但我希望gui同时显示数据:在HAX和加载中1 我怎样才能做到Matlab使循环同时运行,matlab,user-interface,simultaneous,Matlab,User Interface,Simultaneous,不幸的是,我有两个循环。这就是为什么我的代码会运行第一个循环,只有当它完成时,才会运行第二个循环 但我希望gui同时显示数据:在HAX和加载中1 我怎样才能做到 hFigure = figure('units','pixels','position',[300 300 424 470],'menubar','none',... 'name','start processing','numbertitle','off','resize','off'); hAxes = axes(
hFigure = figure('units','pixels','position',[300 300 424 470],'menubar','none',...
'name','start processing','numbertitle','off','resize','off');
hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]);
loading1 = uicontrol('style','text','unit','pix','position',[0 72 424 40],...
'backgroundcolor','r','fontsize',20);
%% shows the data on hAxes
for i = 5:100
if mod(i,2) == 0
set(hAxes,'Color','b');
else
set(hAxes,'Color','g');
end
drawnow;
end
%% shows the data on loading1
for i=1:200
image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
set(loading1,'string',image2);
drawnow;
end
此代码由Peter编写:
function test1
hFigure = figure('units','pixels','position',[300 300 424 470],'menubar','none','name','start processing','numbertitle','off','resize','off');
% Your other setup calls
hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]);
loading1 = uicontrol('style','text','unit','pix','position',[0 72 424 40],'backgroundcolor','r','fontsize',20);
c = 1;
t = timer('TimerFcn', @color_change_fcn,'StartDelay',1.0);
start(t);
for i=1:200
image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
set(loading1,'string',image2);
drawnow;
end
function color_change_fcn
if mod(c,2) == 0
set(hAxes,'Color','b');
else
set(hAxes,'Color','g');
end
drawnow;
c = c + 1;
end
end
它不工作(不显示哈克斯)。我看到它没有运行color\u change\u fcn(我试图在color\u change\u fcn函数的第一行中写入:disp('test'),但它没有打印任何内容。这就是您想要的吗?只需合并循环体即可
for i=1:200
if mod(i,2) == 0
set(hAxes,'Color','b');
else
set(hAxes,'Color','g');
end
image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
set(loading1,'string',image2);
drawnow;
end
编辑:好的,在这种情况下,尝试计时器而不是第一个循环
function output = main_function
% Your other setup calls
hAxes = axes('Parent',hFigure,'Units','pixels','Position',[0 112 424 359]);
c = 0;
t = timer('TimerFcn', @color_change_fcn, 'Period', 1.0);
start(t);
for i=1:200
image2 = horzcat('now processing ', 'image', num2str(i), '.jpg of 200 images');
set(loading1,'string',image2);
drawnow;
end
function color_change_fcn
if mod(c,2) == 0
set(hAxes,'Color','b');
else
set(hAxes,'Color','g');
end
drawnow;
c = c + 1;
end
end
请记住,MATLAB控制流本质上是单线程的,因此如果MATLAB在其他地方忙于工作,这些回调将不会运行。这似乎与您的相关,您希望两个循环同时运行(至少看起来是这样) 在回答的基础上,考虑下面的工作实例:
function timerDemo()
%# prepare GUI
hFig = figure('Menubar','none', 'Resize','off');
axes('XLim',[0 1], 'YLim',[0 1], 'Visible','off', ...
'Units','normalized', 'Position',[0.1 0.2 0.8 0.6])
hTxt = uicontrol('Style','text', 'FontSize',24, ...
'Units','normalized', 'Position',[0 0.9 1 0.1]);
hPatch = patch([0 0 1 1 0],[0 1 0 1 0],'k');
%# colors to cycle through
c = 1;
clr = lines(4);
%# create timer
delay = 0.5;
hTimer = timer('Period',delay, 'StartDelay',delay, ...
'ExecutionMode','FixedRate', 'TimerFcn',@timerCallback);
%# when figure is closed
set(hFig, 'CloseRequestFcn',@onClose);
%# process images
start(hTimer); %# start timer
for i=1:100
if ~ishandle(hFig), break; end
msg = sprintf('Processing image %d / %d', i, 100);
set(hTxt, 'String',msg)
%# some lengthy operation
pause(.1)
end
if isvalid(hTimer)
stop(hTimer) %# stop timer
delete(hTimer) %# delete timer
end
%# timer callback function
function timerCallback(src,evt)
if ~ishandle(hFig), return; end
%# incremenet counter circularly
c = rem(c,size(clr,1)) + 1;
%# update color of patch
set(hPatch, 'FaceColor',clr(c,:));
drawnow
end
%# on figure close
function onClose(src,evt)
%# stop and delete timer
if isvalid(hTimer)
stop(hTimer);
delete(hTimer);
end
%# call default close callback
feval(@closereq)
end
end
该代码模拟了对多个图像运行冗长的处理步骤,同时显示动画以保持用户的娱乐性
为了保持代码简单,我展示了一个补丁,它可以不断更新颜色(使用计时器)。这代表“加载…”的意思
您可以使用并行计算工具箱和parfor构造,但我建议您以某种方式将这两个循环组合起来,以便具有确定性行为。@Ansari,我知道我可以使用Peter在评论中写给我的内容,但我向他解释原因。请阅读我对他的回答,并告诉我是否可以使用并行计算工具做到这一点box.no,我只是粘贴了一个示例。在我的真实代码中,我必须在第二个循环中计算大量数据,在第一个循环中,我希望显示图片(直到用户关闭图形)。因此,如果我知道如何在本例中执行此操作,我将知道如何在我的代码中执行此操作。但感谢您的评论。嗨,peter,我更新了主题的代码。请阅读。请注意,在't=timer(…)'我用'StartDelay'替换了'Period'这个词。我这样做是因为在'Period'选项中,我得到了一个错误:???在为计时器'timer-30'计算TimerFcn时出错,输入参数太多。谢谢。我在大学里有一个关于图像处理的项目。我对图像处理和matlab没有任何知识。谢谢你和一些同事是的,我成功地完成了这个项目的很大一部分。太棒了!所以我要衷心感谢你们!