Matlab 如何在等待用户输入时继续处理

Matlab 如何在等待用户输入时继续处理,matlab,image-processing,input,Matlab,Image Processing,Input,假设我们有以下for循环: for i=1:100 % image processing calculation % show image to the user and ask for his rating rating(i) = input('Enter rating of the shown image\n'); end 显然,input命令将等待用户输入,for循环将在用户提供输入之前不会继续。我在for循环中运行的图像处理代码非常耗时,因此我想知道是否有办法让

假设我们有以下
for
循环:

for i=1:100
    % image processing calculation
    % show image to the user and ask for his rating
    rating(i) = input('Enter rating of the shown image\n');
end

显然,
input
命令将等待用户输入,
for
循环将在用户提供输入之前不会继续。我在
for
循环中运行的图像处理代码非常耗时,因此我想知道是否有办法让MATLAB在等待用户输入的同时在后台继续计算以下循环迭代。

这就是我在评论中提到的想法

num_im = 3; % Number of images

% Initialize
rating = zeros(num_im,1);
fig = zeros(num_im,1);
txt = zeros(num_im,1);

% Main loop
for i = 1:num_im
    % image processing calculation
    pause(1); % a dummy calculation
    % Open a figure
    fig(i) = figure('Name', ['Image #' num2str(i)],...
        'CloseRequestFcn', [... % read the text edit
        'temp = str2double(get(txt(' num2str(i) '), ''String'')); ',...
        'if isnan(temp), errordlg(''Please enter the rating.''); ',... % Error if invalid
        'else, rating(' num2str(i) ') = temp; delete(fig(' num2str(i) ')); end']);

    subplot(2,1,1, 'position', [0 0.5 1 0.5]);

    % Show your image
    [im, map] = imread('trees.tif');
    imshow(im, map);
    % Text edit
    txt(i) = uicontrol('Units','Normalized',...
        'Style','edit',...
        'Position', [0.3 0.2 0.4 0.2]); 
end
输入评级后,确保单击其他地方,以便
文本框
检测您的输入

它将所有评级存储在
评级
矩阵中

但是,这并不限制开放图形的数量。 如果要限制它们,请参阅编辑


编辑 限制打开的数字的数量。我建议您将计算后的所有图像放在队列中,并设置计时器在后台询问用户评分,直到询问所有评分

Main.m tmr_callback.m
%如果队列中有任何未关闭的图像,请创建GUI
%如果开放数字小于最大数字数
如果显示\u idx

我还建议在用户输入每个数据(评级)后将其写入文件。例如,您可以使用。

为什么不在
for
循环之后,在他们自己的
for
循环中询问评分?计算与评分值无关。将分级放在“for”循环之前是不符合逻辑的,因为我的代码对一组100个图像中的一个图像进行特定处理,然后询问用户的分级。如果在for循环处理完成后询问用户的分级,我将在用户等待时浪费时间在所有图像上运行for循环。这就是为什么我在每次迭代结束前询问评分,然后我建议您为每次迭代打开一个新的数字(您可以限制打开数字的最大数量)。在图中沿
文本框显示图像,供用户输入评级。每当用户想要评分时,他/她都会在
文本框中输入评分,当关闭图形时,评分可以保存到
rating(i)
。查看回调函数而不是循环:这正是我所需要的。您建议我如何限制同时打开的数字的最大数量?假设我想同时打开3个图形,然后程序暂停运行,直到其中一个图形关闭。我认为保持一个开放数字的数量变量可以派上用场,但我不知道如何设置最大限制。。。
num_im = 4; % Number of images
max_num_figs = 2; % maximum number of open figures

% Initialize
rating = zeros(num_im,1);
fig = zeros(num_im,1);
txt = zeros(num_im,1);
show_idx = 0; % Number of shown images
Calc_idx = 0; % Number of images in Queue
num_open_figs = 0; % number of open figures

Queue = cell(num_im, 1);
% Main loop

% Start a timer for asking the user rating in background
tmr = timer('ExecutionMode', 'fixedRate',...
    'Period', 0.5, 'TimerFcn', 'tmr_callback');
start(tmr);

for k = 1:num_im
    % image processing calculation
    pause(1); % a dummy calculation

    % Put the image and its data into a Queue ith
    [im, map] = imread('trees.tif');
    Queue{Calc_idx+1} = {im, map};
    Calc_idx = Calc_idx + 1;
end
% Create a GUI if there is any unshown images in the Queue
% and if open figures are less than maximum number of figures
if show_idx < Calc_idx && num_open_figs < max_num_figs
    num_open_figs = num_open_figs + 1;
    show_idx = show_idx + 1; % Number of shown images
    % Open a figure
    fig(show_idx) = figure('Name', ['Image #' num2str(show_idx)],...
        'CloseRequestFcn', [...
        'temp = str2double(get(txt(' num2str(show_idx) '), ''String'')); ',... % Read the text edit
        'if isnan(temp), errordlg(''Please enter the rating.''); ',... % Error if invalid
        'else, rating(' num2str(show_idx) ') = temp; delete(fig(' num2str(show_idx) ')); ',...
        'num_open_figs  = num_open_figs - 1; end']);

    subplot(2,1,1, 'position', [0 0.5 1 0.5]);
    imshow(Queue{show_idx}{:});
    % Text edit
    txt(show_idx) = uicontrol('Units','Normalized',...
        'Style','edit',...
        'Position', [0.3 0.2 0.4 0.2]);
    % turn off the timer if all ratings are asked.
    if (show_idx == num_im)
        stop(tmr);
    end
end