필터 지우기
필터 지우기

Image File Reading, Error with Strings...?

조회 수: 4 (최근 30일)
Daniel Lee
Daniel Lee 2016년 10월 17일
답변: Image Analyst 2016년 10월 18일
I am trying to read an image file (in the same matlab folder as the code).
Right now, my code is:
shapes = {'Tri', 'Rec', 'Cir', 'Oct'};
num = 1:20;
file = strcat(shapes(i), num2str(num(k)), '.png');
I = imread(file);
However, I am getting an error that the file name has to be a string. I also tried making an array like this:
nums = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'};
And instead used
file = strcat(shapes(i), nums(k), '.png');
But still I got the same error.
If anyone can help with this problem it would be great. ^^
P.S. Is there an easy way to import a file that is in a folder that is inside the Current Folder? Thanks.

답변 (1개)

Image Analyst
Image Analyst 2016년 10월 18일
Try this:
shapes = {'Tri', 'Rec', 'Cir', 'Oct'};
num = 1:20;
% Loop over every shape and every number in "num"
for i = 1 : length(shapes)
for k = 1 : length(num)
thisNumber = num(k); % Extract the number.
% Construct the full file name.
thisFileName = sprintf('%s%d.png', shapes{i}, thisNumber);
fullFileName = fullfile(pwd, thisFileName);
fprintf('Looking for %s\n', fullFileName);
% Check if the file exists.
if exist(fullFileName, 'file')
fprintf(' Found %s\n', thisFileName);
thisImage = imread(file);
% Now do something with thisImage.
else
fprintf(' Did not find %s\n', thisFileName);
end
end
end

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by