MATLAB Answers

add value in array

23 views (last 30 days)
Oman Wisni
Oman Wisni on 29 Oct 2018
Commented: Oman Wisni on 29 Oct 2018
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

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 29 Oct 2018
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 Comments

Oman Wisni
Oman Wisni on 29 Oct 2018
Thanks sir, if the image more than 4 how to declarad it? I mean the name of image not declarad, is automatically show when I input image
KSSV
KSSV on 29 Oct 2018
files = dir('*.bmp') ;
This will take all bmp file names.
Oman Wisni
Oman Wisni on 29 Oct 2018
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');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by