gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:25 28 4
gpt4 key购买 nike

假设我们有以下 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

确保在输入评分后单击其他地方,以便文本框检测到您的输入。

它将所有评分存储在rating矩阵中。

然而,这并不限制开放数字的数量。如果您想限制它们,请参阅编辑


编辑

限制打开图形的数量。我建议你将计算后的所有图像放入一个队列中,并设置一个计时器在后台询问用户评分,直到询问完所有评分。

主.m

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

tmr_callback.m

% 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

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

关于matlab - 如何在等待用户输入时matlab继续处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com