2012-06-16 4 views
1

불행하게도 두 개의 루프가 있습니다. 이것이 내 코드가 첫 번째 루프를 실행하는 이유이며 두 번째 루프가 끝나면 완료됩니다.Matlab은 루프를 동시에 실행합니다.

그러나 나는 hAxes 및 loading1에서 동시에 데이터를 표시하려고합니다.

어떻게 만들 수 있습니까?

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 

이 코드는 피터입니다 :

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 

그것은합니다 (hAxes 표시되지 않습니다) 작동하지 않습니다. Color_change_fcn 함수가 실행되지 않았다는 것을 알았습니다. (color_change_fcn 함수의 첫 번째 행에 disp ('test')를 쓰려고했지만 아무 것도 출력하지 않습니다.

+1

당신은 병렬 컴퓨팅 도구 상자와 parfor 구조를 사용할 수 있습니다,하지만 난 당신이 결정적 행동을 가질 수 있도록 당신이 어떻게 든 두 개의 루프를 결합 좋습니다. – Ansari

+0

@Ansari, 나는 Peter가 그의 코멘트에서 쓴 것을 사용할 수 있음을 알고 있지만 그에게 이유를 설명합니다. 제 대답을 읽어 주시고 제가 병렬 컴퓨팅 도구 상자로 할 수 있는지 말해주십시오. –

답변

1

이것은 두 개의 루프를 동시에 실행하려는 사용자의 previous question과 관련이있는 것으로 보입니다 (적어도 그렇게 잘 보이는 것처럼 보입니다).

건물 @Peter의 대답에, 다음과 같은 작업 예제를 고려하십시오

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 

코드는 동시에 사용자를 유지하기 위해 애니메이션을 보여 주면서, 이미지의 수에 긴 처리 단계를 실행 시뮬레이션 즐겁게.

코드를 간단하게 유지하기 위해 타이머를 사용하여 색상을 지속적으로 업데이트하는 패치를 보여 드리고 있습니다. 이것은 animated GIF image의 "로드 중 ..."을 의미합니다.

screenshot

+0

저는 대학에서 이미지 프로세싱에 관한 프로젝트를 가지고 있습니다. 나는 이미지 처리 및 matlab에 대한 지식이 없다. 당신과 몇몇 사람들 덕분에이 프로젝트의 큰 부분을 수행하는 데 성공했습니다. 놀랍다! 그래서 나는 내 마음으로 너에게 감사 드리고 싶다. –

1

원하는 것은이게 뭡니까?

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 

편집 :. OK,이 경우, 대신 첫 번째 루프의 타이머를 시도 그냥 MATLAB 제어 흐름이 본질적으로 단일 스레드 있다는 사실을, 그래서

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이 다른 곳에서 작업 중일 경우 실행하십시오.

+0

아니요, 샘플을 붙여 넣기 만하면됩니다. 내 실제 코드에서는 두 번째 루프에서 많은 데이터를 계산해야하며 첫 번째 루프에서는 그림을 표시하려고합니다 (사용자가 그림을 닫을 때까지). 그래서 만약 내가이 예제에서 그것을하는 법을 안다면, 나는 나의 코드에서 그것을하는 법을 알 것이다. 그러나 당신의 코멘트를 당신을 감사하십시오. –

+0

안녕하세요, 필자는 내 주제 코드를 업데이트했습니다. 그것을 읽으십시오. 't = timer (...)'에서 'Period'를 'StartDelay'로 바 꾸었습니다. 나는 'Period'옵션에서 오류가 발생했기 때문에 그렇게한다 : ??? 타이머 'timer-30'에 대해 TimerFcn을 평가하는 동안 오류가 발생했습니다. 입력 인수가 너무 많습니다. 감사합니다. –