ConvertToC.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. function ConvertToC(fileName)
  2. delimiter = '';
  3. %% Format string for each line of text:
  4. % column1: text (%s)
  5. % For more information, see the TEXTSCAN documentation.
  6. formatSpec = '%s%[^\n\r]';
  7. %% Open the text file.
  8. fileID = fopen(strcat(fileName,'.m'),'r');
  9. %% Read columns of data according to format string.
  10. % This call is based on the structure of the file used to generate this
  11. % code. If an error occurs for a different file, try regenerating the code
  12. % from the Import Tool.
  13. dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false, 'Bufsize', 65535);
  14. %% Close the text file.
  15. fclose(fileID);
  16. %% Create output variable
  17. SymbolicOutput = [dataArray{1:end-1}];
  18. %% Clear temporary variables
  19. clearvars filename delimiter formatSpec fileID dataArray ans;
  20. %% Convert indexing and replace brackets
  21. % replace 1-D indexes
  22. for arrayIndex = 1:99
  23. strIndex = int2str(arrayIndex);
  24. strRep = sprintf('[%d]',(arrayIndex-1));
  25. strPat = strcat('\(',strIndex,'\)');
  26. for lineIndex = 1:length(SymbolicOutput)
  27. str = char(SymbolicOutput(lineIndex));
  28. SymbolicOutput(lineIndex) = {regexprep(str, strPat, strRep)};
  29. end
  30. end
  31. % replace 2-D left indexes
  32. for arrayIndex = 1:99
  33. strIndex = int2str(arrayIndex);
  34. strRep = sprintf('[%d,',(arrayIndex-1));
  35. strPat = strcat('\(',strIndex,'\,');
  36. for lineIndex = 1:length(SymbolicOutput)
  37. str = char(SymbolicOutput(lineIndex));
  38. SymbolicOutput(lineIndex) = {regexprep(str, strPat, strRep)};
  39. end
  40. end
  41. % replace 2-D right indexes
  42. for arrayIndex = 1:99
  43. strIndex = int2str(arrayIndex);
  44. strRep = sprintf(',%d]',(arrayIndex-1));
  45. strPat = strcat('\,',strIndex,'\)');
  46. for lineIndex = 1:length(SymbolicOutput)
  47. str = char(SymbolicOutput(lineIndex));
  48. SymbolicOutput(lineIndex) = {regexprep(str, strPat, strRep)};
  49. end
  50. end
  51. % replace commas
  52. for lineIndex = 1:length(SymbolicOutput)
  53. str = char(SymbolicOutput(lineIndex));
  54. SymbolicOutput(lineIndex) = {regexprep(str, '\,', '][')};
  55. end
  56. %% replace . operators
  57. for lineIndex = 1:length(SymbolicOutput)
  58. strIn = char(SymbolicOutput(lineIndex));
  59. strIn = regexprep(strIn,'\.\*','\*');
  60. strIn = regexprep(strIn,'\.\/','\/');
  61. strIn = regexprep(strIn,'\.\^','\^');
  62. SymbolicOutput(lineIndex) = cellstr(strIn);
  63. end
  64. %% Replace ^2
  65. % replace where adjacent to ) parenthesis
  66. for lineIndex = 1:length(SymbolicOutput)
  67. idxsq = regexp(SymbolicOutput(lineIndex),'\)\^2');
  68. if ~isempty(idxsq{1})
  69. strIn = SymbolicOutput(lineIndex);
  70. idxlp = regexp(strIn,'\(');
  71. idxrp = regexp(strIn,'\)');
  72. for pwrIndex = 1:length(idxsq{1})
  73. counter = 1;
  74. index = idxsq{1}(pwrIndex);
  75. endIndex(pwrIndex) = index;
  76. while (counter > 0 && index > 0)
  77. index = index - 1;
  78. counter = counter + ~isempty(find(idxrp{1} == index, 1));
  79. counter = counter - ~isempty(find(idxlp{1} == index, 1));
  80. end
  81. startIndex(pwrIndex) = index;
  82. % strPat = strcat(strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)),'^2');
  83. strRep = strcat('sq',strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)));
  84. % cellStrPat(pwrIndex) = cellstr(strPat);
  85. cellStrRep(pwrIndex) = cellstr(strRep);
  86. end
  87. for pwrIndex = 1:length(idxsq{1})
  88. strRep = char(cellStrRep(pwrIndex));
  89. strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)+2) = strRep;
  90. end
  91. SymbolicOutput(lineIndex) = strIn;
  92. end
  93. end
  94. % replace where adjacent to ] parenthesis
  95. for lineIndex = 1:length(SymbolicOutput)
  96. strIn = char(SymbolicOutput(lineIndex));
  97. [match,idxsq1,idxsq2] = regexp(strIn,'\w*\[\w*\]\^2','match','start','end');
  98. [idxsq3] = regexp(strIn,'\[\w*\]\^2','start');
  99. if ~isempty(match)
  100. for pwrIndex = 1:length(match)
  101. strVar = strIn(idxsq1(pwrIndex):idxsq3(pwrIndex)-1);
  102. strIndex = strIn(idxsq3(pwrIndex)+1:idxsq2(pwrIndex)-3);
  103. strPat = strcat(strVar,'\[',strIndex,'\]\^2');
  104. strRep = strcat('sq(',strVar,'[',strIndex,']',')');
  105. strIn = regexprep(strIn,strPat,strRep);
  106. end
  107. SymbolicOutput(lineIndex) = cellstr(strIn);
  108. end
  109. end
  110. % replace where adjacent to alpanumeric characters
  111. for lineIndex = 1:length(SymbolicOutput)
  112. strIn = char(SymbolicOutput(lineIndex));
  113. [match,idxsq1,idxsq2] = regexp(strIn,'\w*\^2','match','start','end');
  114. [idxsq3] = regexp(strIn,'\^2','start');
  115. if ~isempty(match)
  116. for pwrIndex = 1:length(match)
  117. strVar = strIn(idxsq1(pwrIndex)+2*(pwrIndex-1):idxsq2(pwrIndex)-2+2*(pwrIndex-1));
  118. strPat = strcat(strVar,'\^2');
  119. strRep = strcat('sq(',strVar,')');
  120. strIn = regexprep(strIn,strPat,strRep);
  121. end
  122. SymbolicOutput(lineIndex) = cellstr(strIn);
  123. end
  124. end
  125. %% Replace ^(1/2)
  126. % replace where adjacent to ) parenthesis
  127. for lineIndex = 1:length(SymbolicOutput)
  128. idxsq = regexp(SymbolicOutput(lineIndex),'\)\^\(1\/2\)');
  129. if ~isempty(idxsq{1})
  130. strIn = SymbolicOutput(lineIndex);
  131. idxlp = regexp(strIn,'\(');
  132. idxrp = regexp(strIn,'\)');
  133. for pwrIndex = 1:length(idxsq{1})
  134. counter = 1;
  135. index = idxsq{1}(pwrIndex);
  136. endIndex(pwrIndex) = index;
  137. while (counter > 0 && index > 0)
  138. index = index - 1;
  139. counter = counter + ~isempty(find(idxrp{1} == index, 1));
  140. counter = counter - ~isempty(find(idxlp{1} == index, 1));
  141. end
  142. startIndex(pwrIndex) = index;
  143. % strPat = strcat(strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)),'^2');
  144. strRep = strcat('(sqrt',strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)),')');
  145. % cellStrPat(pwrIndex) = cellstr(strPat);
  146. cellStrRep(pwrIndex) = cellstr(strRep);
  147. end
  148. for pwrIndex = 1:length(idxsq{1})
  149. strRep = char(cellStrRep(pwrIndex));
  150. strIn{1}(startIndex(pwrIndex):endIndex(pwrIndex)+6) = strRep;
  151. end
  152. SymbolicOutput(lineIndex) = strIn;
  153. end
  154. end
  155. %% Replace Divisions
  156. % Compiler looks after this type of optimisation for us
  157. % for lineIndex = 1:length(SymbolicOutput)
  158. % strIn = char(SymbolicOutput(lineIndex));
  159. % strIn = regexprep(strIn,'\/2','\*0\.5');
  160. % strIn = regexprep(strIn,'\/4','\*0\.25');
  161. % SymbolicOutput(lineIndex) = cellstr(strIn);
  162. % end
  163. %% Convert declarations
  164. for lineIndex = 1:length(SymbolicOutput)
  165. str = char(SymbolicOutput(lineIndex));
  166. if ~isempty(regexp(str,'zeros', 'once'))
  167. index1 = regexp(str,' = zeros[','once')-1;
  168. index2 = regexp(str,' = zeros[','end','once')+1;
  169. index3 = regexp(str,'\]\[','once')-1;
  170. index4 = index3 + 3;
  171. index5 = max(regexp(str,'\]'))-1;
  172. str1 = {'float '};
  173. str2 = str(1:index1);
  174. str3 = '[';
  175. str4 = str(index2:index3);
  176. str4 = num2str(str2num(str4)+1);
  177. str5 = '][';
  178. str6 = str(index4:index5);
  179. str6 = num2str(str2num(str6)+1);
  180. str7 = '];';
  181. SymbolicOutput(lineIndex) = strcat(str1,str2,str3,str4,str5,str6,str7);
  182. end
  183. end
  184. %% Change covariance matrix variable name to P
  185. for lineIndex = 1:length(SymbolicOutput)
  186. strIn = char(SymbolicOutput(lineIndex));
  187. strIn = regexprep(strIn,'OP\[','P[');
  188. SymbolicOutput(lineIndex) = cellstr(strIn);
  189. end
  190. %% Write to file
  191. fileName = strcat(fileName,'.cpp');
  192. fid = fopen(fileName,'wt');
  193. for lineIndex = 1:length(SymbolicOutput)
  194. fprintf(fid,char(SymbolicOutput(lineIndex)));
  195. fprintf(fid,'\n');
  196. end
  197. fclose(fid);
  198. clear all;