How to use multiple user input variable?

10 次查看(过去 30 天)
Hello everyone,
I want you to help me how to solve this appeared error when i run this code.
First, i want to use video capture from web.
2. And then i want to ask: Do you want to save image from webcam?
3. I want to input dialog box for input variable (ImageName, Num_images, Start_num_Im).
4. And the i want to write this capture images to database with this input variables.
Please help me how to solve appeared error.
Thank you very much.
for my sample code:
clear all
close all
clc
% Create the webcam object.
cam = webcam(1, 'Resolution','640x480');
preview(cam);
count = 0;
promptMessage = sprintf('Do you want to capture frame from webcam?');
button = questdlg(promptMessage, ...
'Save individual frames?',...
'Yes', 'No', 'No');
hold on;
%compare the string
if strcmp (button, 'Yes')
x = inputdlg({'Name','Number_of_images','start_number'},...
'Please enter the Imageinfo', [1 20; 1 20; 1 20]);
videoFrame = snapshot(cam);
figure, imshow (videoFrame);
%this is where we will be save face image
baseDir = 'C:\Program Files\MATLAB\R2018a\bin\testimages\' + Name;
% Finally, create the folder if it doesn't exist already.
if ~exist(baseDir, 'dir')
mkdir(baseDir);
end
%baseName = image
baseName = join(baseDir + num2str(start_number) + '.jpg');
%check exit file or not!
while Num_images < Number_of_images
Num_images = count + 1;
baseName = join(baseDir + num2str(start_number) + '.jpg');
Iresize = imresize (videoFrame, [500 500]);
imwrite (Iresize, baseName);
%figure();
imshow (Iresize);
title ('Capture image');
end
elseif strcmp (button, 'No')
disp ('Ok! close all');
closePreview(cam);
clear cam;
end
  1 个评论
Rik
Rik 2020-10-1
  • Replace clear all with clear or clearvars. Or even better: use functions to keep your workspace clean. Don't ignore the warning mlint is giving you.
  • Don't use close all. Maybe your user has useful GUIs or figures open. You aren't shutting down Matlab or the computer at the end of the code either. You should be using explicit handles to a figure so you can close that figure at the end of your code.
  • You don't print anything to the command window, so why are you calling clc?
You didn't mention which line is triggering an error, and what the full error message is.

请先登录,再进行评论。

回答(1 个)

Subhadeep Koley
Subhadeep Koley 2020-10-1
编辑:Subhadeep Koley 2020-10-1
Alex, the below code might help! Have a look.
% Create the webcam object.
cam = webcam(1, 'Resolution', '640x480');
preview(cam);
promptMessage = sprintf('Do you want to capture frame from webcam?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No',...
'No');
% Compare the string
if strcmp (button, 'Yes')
x = inputdlg({'Name', 'Number_of_images', 'Start_number'},...
'Please enter the image info', [1 20; 1 20; 1 20]);
name = x{1};
numberOfImages = str2double(x{2});
startNumber = str2double(x{3});
% Paste your dir path here
baseDir = 'C:\testImages\';
% Finally, create the folder if it doesn't exists already
if ~exist(baseDir, 'dir')
mkdir(baseDir);
end
figHan = figure;
axHan = axes(figHan);
for idx = 1:numberOfImages
videoFrame = snapshot(cam);
imshow(videoFrame, 'Parent', axHan)
title(['Frame number: ', num2str(idx)])
videoFrame = imresize(videoFrame, [500, 500]);
baseName = [baseDir, name, num2str(startNumber), '.jpg'];
imwrite (videoFrame, baseName);
startNumber = startNumber + 1;
end
clc
disp ('Done!')
closePreview(cam)
clear cam
elseif strcmp (button, 'No')
disp ('Ok! Closed all');
closePreview(cam)
clear cam
end
  4 个评论
Alex Zai
Alex Zai 2020-10-2
编辑:Alex Zai 2020-10-2
Great! That's help to me. Thanks for your answers.
I have another question, pls. In this code, if input value for x isempty (user din't entered nothing ), can i use this?:
if isempty(x)
disp ('You did not entered the input values');
closePreview(cam)
clear cam
end
I added after input dialog bo and tried. But it still running loop and appered error. How can i fix this pls?
Have a great day!
Rik
Rik 2020-10-2
If you have this as a function you could use return at the end of the if section. Unfortunately you are now forced to put the rest of your loop in the else section.
Another strategy would be to put the actual code in a try part, and put this in the catch block. Then you can trigger errors in places you need to exit the code. That way you don't need to copy and paste that code many times.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by