Facing difficulty in converting a matlab function to c code
1 view (last 30 days)
Show older comments
%#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
Walter Roberson
on 5 Sep 2019
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 '.'
Answers (1)
Subhadeep Koley
on 5 Sep 2019
- When generating C code, all variable must be initialized and declared at their first occurrence.
- You can only generate standalone C/C++ code for functions that are supported by the Codegen. You may either need to write your own C implementation for the unsupported function, or use MATLAB Compiler to deploy your MATLAB code.
- 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.
1 Comment
Walter Roberson
on 5 Sep 2019
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!