How to convert char data to num??

91 views (last 30 days)
saeeda saher
saeeda saher on 22 Jun 2018
Answered: Adam Danz on 22 Jun 2018
I have .mat file which contains labels of my image dataset. These labels are numbers (0,1,2,3,4,5,6) but in mat file these are saved as characters ('0','1','2','3','4','5','6'). How to convert these chars to num??
% Features Extraction of Face based on HOG
clear;
testing=imageSet('Testing\','recursive'); % folder name of the database
K=1;
for i=1:size(testing,2 )
for j=1:testing(i).Count
Face=read(testing(i),j);
Face=imresize(Face, [48 48]);
HOG_Features=extractHOGFeatures(Face);
testingFeatures(K,:)=single(HOG_Features);
testinglabel{K}=testing(i).Description;
K=K+1;
end
persons{i}=testing(i).Description;
end
testinglabel=testinglabel';
csvwrite(' Testing.csv', testingFeatures)

Answers (1)

Adam Danz
Adam Danz on 22 Jun 2018
In your example, "characters ('0','1','2','3','4','5','6')" it looks like the labels might be a cell array of strings. For example
c = {'1','2','3','4'};
If that's the case, you can use
d = str2double(c);
Here's the conversion if the labels are a single string of numbers.
% single string of numbers
c = '1 2 3 4 5';
d = str2double(strsplit(c));

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!