add value in array

1 次查看(过去 30 天)
Oman Wisni
Oman Wisni 2018-10-29
评论: Oman Wisni 2018-10-29
hi,
I have foldername of image, the name of image is a1.bmp, a2.bmp, b1.bmp, b2.bmp.
I want store in array when a1 & a2.bmp the value is 1, and b1&b2.bmp the value is 2.
Here the illustration :
How can I do it in matlab ? thanks

采纳的回答

KSSV
KSSV 2018-10-29
files = {'a1.bmp' ; 'a2.bmp' ; 'b1.bmp' ; 'b2.bmp' } ;
N = length(files) ;
iwant = zeros(N,1) ;
letter2number = @(c)1+lower(c)-'a';
for i = 1:N
[filepath,name,ext] = fileparts(files{i}) ;
k = letter2number(name) ;
iwant(i) = k(1) ;
end
  3 个评论
KSSV
KSSV 2018-10-29
files = dir('*.bmp') ;
This will take all bmp file names.
Oman Wisni
Oman Wisni 2018-10-29
Sir, I already trying it, but when I input another image with different name the result before replace. I have 11 species of leaf, every species contain with 25 image. I want to save it without replacing the value. Here I attached the image what I mean and here I have code for it but still error.
try
load Target.mat
catch me
end
if exist('target','var')~=0
height = size(Input,1);
else
%Input = [];
IDName=[];
end
ID = cellstr(foldername); %String
IDName = [IDName;ID];
target = zeros(1,275);
target(1:25,:) = 1;
target(26:50,:) = 2;
target(51:75,:) = 3;
target(76:100,:) = 4;
target(101:125,:) = 5;
target(121:150,:) = 6;
target(151:175,:) = 7;
target(176:200,:) = 8;
target(201:225,:) = 9;
target(226:250,:) = 10;
target(251:275,:) = 11;
Targettable.Properties.VariableNames = target;
save('target.mat','target','targettable','IDName');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by