- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我们有以下 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
矩阵中。
然而,这并不限制开放数字的数量。如果您想限制它们,请参阅编辑。
限制打开图形的数量。我建议你将计算后的所有图像放入一个队列中,并设置一个计时器在后台询问用户评分,直到询问完所有评分。
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
我还建议在用户输入每个数据后将数据(评级)写入文件。您可以使用 dlmwrite
例如。
关于matlab - 如何在等待用户输入时matlab继续处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016023/
我试图让脚本暂停大约 1 秒,然后继续执行脚本,但我似乎无法弄清楚如何做。这是我的代码: function hello() { alert("Hi!") //I need about a 1
wait() 和 wait(timeout) 之间有什么区别。无论如何 wait() 需要等待通知调用,但为什么我们有 wait(timeout)? 那么 sleep(timeout) 和 wait(
我需要做什么: 我有一个带有文件输入和隐藏文本输入的上传表单。用户上传图像,图像被操作,然后发送到远程服务器进行处理,这需要几秒钟,然后远程服务器将最终的图像发送回家庭服务器,并保存在新文件夹中。 J
大家好,我正在使用 Visual C++ 2010,尝试使用 Winsock 编写服务器/客户端应用程序...我不确定为什么,但有时服务器会在 listen() 函数处等待,有时会在 accept 处
任务描述 我为我的 Angular 应用程序实现了 CRSF 保护。服务器检查 crsf token 是否位于请求的 header “X-CSRF-TOKEN”中。如果不是,它会发送一个 HTTP 响
我想做这个例子https://stackoverflow.com/a/33585993/1973680同步。 这是正确的实现方式吗? let times= async (n,f)=>{
我如何将 while 循环延迟到 1 秒间隔,而不会将其运行的整个代码/计算机的速度减慢到一秒延迟(只是一个小循环)。 最佳答案 Thread.sleep(1000); // do nothing f
我知道这是一个重复的问题。但是我无法通过解释来理解。我想用一个很好的例子来清楚地理解它。任何人都可以帮忙吗。 “为什么我们从同步上下文中调用 wait()、notify() 方法”。 最佳答案 当我们
我有一个 click 事件,该事件是第一次从另一个地方自动触发的。我的问题是它运行得太快,因为所需的变量仍在由 Flash 和 Web 服务定义。所以现在我有: (function ($) {
我有如下功能 function async populateInventories(custID){ this.inventories = await this.inventoryServic
我一直对“然后”不被等待的行为感到困扰,我明白其原因。然而,我仍然需要绕过它。这是我的用例。 doWork(family) { return doWork1(family)
我想我理解异步背后的想法,返回一个Future,但是我不清楚异步在一个非常基本的层面上如何表现。据我了解,它不会自动在程序中创建异步行为。例如: import 'dart:async'; main()
我正在制作一个使用异步的Flutter应用程序,但它的工作方式不像我对它的了解。所以我对异步和在 Dart 中等待有一些疑问。这是一个例子: Future someFunction() async {
我在 main.tf 中创建资源组和 vNet,并在同一文件中引用模块。问题是,模块无法从模块访问这些资源。相关代码(删除了大部分代码,只留下相关部分): main.tf: module "worke
我的代码的问题是,当代码第一次运行时,我试图获取的 dom 元素并不总是存在,如果它不存在,那么永远不会做出 promise 。 我是否可以等到 promise 做出后再尝试实现它? 我希望我的最后一
所以,过去几天我一直在研究这段代码,并尝试实现回调/等待/任何需要的东西,但没有成功。 问题是,我如何等待响应,直到我得到两个函数的回调? (以及我将如何实现) 简而言之,我想做的是: POST 发生
谁能帮我理解这一点吗? 如果我们有一个类: public class Sample{ public synchronized method1(){ //Line1 .... wait();
这是我编写的代码,用于测试 wait() 和 notify() 的工作。现在我有很多疑问。 class A extends Thread { public void run() { try
我有以下代码由于语法错误而无法运行(在异步函数外等待) 如何使用 await 定义变量并将其导出? 当我这样定义一个变量并从其他文件导入它时,该变量是只创建一次(第一次读取文件时?)还是每次导入时都创
一个简单的线程程序,其中写入器将内容放入堆栈,读取器从堆栈中弹出。 java.util.Stack; import java.util.concurrent.ExecutorService; impo
我是一名优秀的程序员,十分优秀!