4

Suppose we have the following for loop:

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

Obviously, the input command will wait for the user input and the for loop won't continue until the user has provided his input. The image processing code I'm running in the for loop is time consuming so I'd like to know if there's a way to make MATLAB continue calculations - in the background - for the following loop iteration while waiting for the user's input.

5
  • Why not ask for the ratings after the for loop, in their own for loop? Commented Jun 10, 2013 at 2:13
  • The calculations are independent of the value of rating. Putting the rating before 'for' loop is not logical, because my code does a certain processing on an image from a set of 100 images and then asks the user for his rating. Commented Jun 10, 2013 at 2:14
  • If I ask the user for the rating after the for loop is done processing, i will be wasting time running the for loop on all images while the user is waiting. That's why I went to ask for the rating by the end of every iteration Commented Jun 10, 2013 at 2:16
  • Then I suggest you to open a new figure for each iteration (you can limit the maximum number of open figures). Show the image inside the figure along a text box for the user to enter the rating. Whenever the user wants to rate, he/she enters the rating in the text box and when closes the figure and the rating could be saved into rating(i). Commented Jun 10, 2013 at 2:22
  • 1
    look at callback functions instead of for loops : mathworks.com/help/matlab/matlab_prog/… Commented Jun 10, 2013 at 2:27

1 Answer 1

3

This is the idea I mentioned in my comment.

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

Make sure to click somewhere else after you enter the ratings so that the text box detects your input.

It will store all the ratings in rating matrix.

However this does not limit the number of open figures. Refer to the Edit if you want to limit them.


Edit

To limit the number of opened figures. I suggest you to put all the images after calculation in a Queue and set a timer to ask the user rating in the background until all the ratings are asked.

Main.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

I also suggest to write the data(rating) into a file after the user enters each of them. You can use dlmwrite for example.

Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much what I need. How do you suggest I limit the maximum number of simultaneously open figures ? Let's say I want to have 3 figures open at the same time, then the program pauses from running until one of the figures is closed. I think keeping a variable of the number of opened figures can come into handy but I don't know how to set a maximum limit ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.