how to convert a structure array to numeric matrix

I am trying to use fitcsvm function in Matlab 2014a to classify the features. I used Md1=fitcsvm(X,Y) to create a svm model where X is a array of feature images of .mat extension and Y is label array. now I am having error that X should be a numeric matrix.

4 Comments

"X is a array of feature images of .mat extension"
.mat files are files that can store many variables, and are saved on a harddrive/cloud/.... .mat files are not variables in the MATLAB workspace.
If you want to use a variable that is saved in a .mat file then you will need to load the variable from the .mat file into the MATLAB workspace, and then you can use it in your code. Read the load documentation for more information.
Asharani Shinde's "Answer" moved here:
Thank you Stephen sir for reply . I already used load to assign my .mat feature file to X variable. and made the array of .mat file . In work-space it shows the X variable as structure type array. My error is X should be a numeric matrix. so now I need to convert the structure array to numeric matrix to work the fitcsvm builtin function in matlab. I am giving here the code which I am trying to execute.
global lm tm X nam Y k ltp_lower Md1 lbl r
co=0;
u=0;
lbl=[char];
r=0;
X=[];
Y=[];
TrainF=dir('tst\');
Train_N = 0;
nam=[];
for k = 1:size(TrainF,1)
if not(strcmp(TrainF(k).name,'.')|strcmp(TrainF(k).name,'..')|strcmp(TrainF(k).name,'Thumbs.db'))
Train_N = Train_N + 1; % Number of all images in the training database
nam{Train_N}=TrainF(k).name;
end
end
for lm=1:length(nam)
tm=nam{lm};
load(['tst\',tm]);
k=ltp_lower;
k_pts = detectSURFFeatures(k);
[boxFeatures, k_pts] = extractFeatures(k, k_pts);
% X=k;
X(:,:,lm)=boxFeatures;
r =tm(7);
if r=='1'
lbl{lm} ='I';
elseif r=='2'
lbl{lm} = 'II';
elseif r=='3'
lbl{lm} = 'III';
elseif r=='4'
lbl{lm} = 'IV';
elseif r=='5'
lbl{lm} = 'V';
elseif r=='6'
lbl{lm} = 'VI';
elseif r=='7'
lbl{lm} = 'VII';
elseif r=='8'
lbl{lm} ='VIII';
elseif r=='9'
lbl{lm} = 'XI';
end
Y=lbl ;
Y=categorical(Y);
Md1=fitcsvm(X,Y);
end
Asharani Shinde's comment moved here:
Thus pls suggest me proper way to train the SVM and how to give test image to trained classifier and what will be the output? either it will give me matching class or matching image to query image.
"In work-space it shows the X variable as structure type array"
You might just need to get one of the fields of X. Learn about structures by reading the MATLAB documentation:
especially the part on how to access the data in a structure.

Sign in to comment.

Answers (0)

Tags

Asked:

on 22 Dec 2017

Edited:

on 24 Dec 2017

Community Treasure Hunt

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

Start Hunting!