MATLAB Answers

0

Facing difficulty in converting a matlab function to c code

Asked by Ansh Mehta on 30 Aug 2019
Latest activity Commented on by Walter Roberson
on 5 Sep 2019
%#codegen
function myModel()
train_path ='data/train';
test_path = 'data/test';
num_clusters = 100;
count=0;
count2=0;
train_data={}
%for loop create words array
%Initialization
d=struct([]);
words={};
pt='';
f=struct([]);
files={};
%______________
d = dir(train_path);
words = {d.name};
words = words(3:end);
xyz=0;
label={};
for i=1:length(words)
pt = char(strcat(train_path,'/',string(words(i))));
f = dir(pt);
files = {f.name};
files = files(3:end);
IM={};
for k=1:length(files)
Iinitial = imread(strcat(pt,'/',files{k}));
[rows, columns, numberOfColorChannels] = size(Iinitial);
if numberOfColorChannels > 1
% It's a true color RGB image. We need to convert to gray
% scale.
Igray = rgb2gray(Iinitial);
else
% It's already gray scale. No need to convert.
Igray = Iinitial;
end
IM{k} = Igray;
xyz = xyz+1
label{xyz}=i
end
disp("length"+length(IM))
train_data{i}=IM;
end
y={};
descriptors={};
v = {};
ct=1;
for i=1:length(train_data)
for j=1:length(train_data{i})
y{ct}=i;
kp=detectORBFeatures(train_data{i}{j});
des=extractFeatures(train_data{i}{j},kp);
features = des.Features;
descriptors{ct}=des;
v =vertcat(v,features);
ct=ct+1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
On using Matlab Coder, after the check for issue thing, I am getting this error and unable to rectify it.
How can we define functions or variables inside a function in matlab?? Also, I tried to initialize some variables as 0 or null form but none of them worked.
Somebody please help me out with this issue...

  1 Comment

It is a mistake to do a dir() and take entries 3 onwards. The order of entries returned by dir() is not defined by MATLAB, and it is not defined by MS Windows either: it is defined by the filesystem layer, which is probably NTFS. NTFS does not formally define the order entries are returned either, but in practice it seems to return them by sorting on byte value of the file name characters. The files '.' and '..' tend to appear first because they sort before most other printable characters. However, the characters !"#$%&'()*+,- sort before '.' does, and some of those are valid in file names. For example a file named '$500.jpg' would sort before '.'

Sign in to comment.

1 Answer

Answer by Subhadeep Koley on 5 Sep 2019

Below are some points to follow when generating C/C++ code from MATLAB code using MATLAB Coder.
  • When generating C code, all variable must be initialized and declared at their first occurrence.
  • The code generated by MATLAB Compiler is not completely standalone (like that generated by MATLAB Coder), but depends on MATLAB run-time libraries, and therefore requires that each target machine either have the same version of MATLAB installed or have the corresponding version of the MATLAB Runtime Compiler installed.
This example shows how to build a C executable from MATLAB Code.

  1 Comment

Notice that code generation for imread
  • Supports reading of 8-bit JPEG images only. The input argument filename must be a valid absolute path or relative path.

Sign in to comment.