ConvertToM.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function ConvertToM(nStates)
  2. %% Initialize variables
  3. fileName = strcat('SymbolicOutput',int2str(nStates),'.txt');
  4. delimiter = '';
  5. %% Format string for each line of text:
  6. % column1: text (%s)
  7. % For more information, see the TEXTSCAN documentation.
  8. formatSpec = '%s%[^\n\r]';
  9. %% Open the text file.
  10. fileID = fopen(fileName,'r');
  11. %% Read columns of data according to format string.
  12. % This call is based on the structure of the file used to generate this
  13. % code. If an error occurs for a different file, try regenerating the code
  14. % from the Import Tool.
  15. dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false,'Bufsize',65535);
  16. %% Close the text file.
  17. fclose(fileID);
  18. %% Create output variable
  19. SymbolicOutput = [dataArray{1:end-1}];
  20. %% Clear temporary variables
  21. clearvars filename delimiter formatSpec fileID dataArray ans;
  22. %% replace brackets and commas
  23. for lineIndex = 1:length(SymbolicOutput)
  24. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_l_', '(');
  25. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_c_', ',');
  26. SymbolicOutput(lineIndex) = regexprep(SymbolicOutput(lineIndex), '_r_', ')');
  27. end
  28. %% Write to file
  29. fileName = strcat('M_code',int2str(nStates),'.txt');
  30. fid = fopen(fileName,'wt');
  31. for lineIndex = 1:length(SymbolicOutput)
  32. fprintf(fid,char(SymbolicOutput(lineIndex)));
  33. fprintf(fid,'\n');
  34. end
  35. fclose(fid);
  36. clear all;
  37. end