Guest User

Untitled

a guest
Aug 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. function realMadLib(file,struct)
  2. %Open the file
  3. fh=fopen(file,'r');
  4. %Edit the name of the file that we will be writing to
  5. [file1 edited]=strtok(file,'.');
  6. newfilename=[file1 '_edited.txt'];
  7. %Open a new file that's titled the way we need it to be
  8. fh1=fopen(newfilename,'w');
  9. %Initialize the empty string so that the while loop can start
  10. str='';
  11. %Make a cell array of nouns, verbs and adjectives so they can be indexed
  12. %out
  13. nouns={struct.noun};
  14. verbs={struct.verb};
  15. adjectives={struct.adj};
  16. %start the generic loop used for low level file I/O, let it run until the
  17. %string is not of type char anymore
  18. while ischar(str)
  19. str=fgets(fh);
  20. %Read the first line of the file, the loop will then read the second,
  21. %third, etc.
  22. rest=str;
  23. %If the line contains characters, run another loop
  24. if ischar(str)
  25. for ind=1:length(str)
  26. %Check the string, letter by letter to see if it is one of the
  27. %symbols we are using
  28. if str(ind)=='@'
  29. %If the character is a @, take it out and replace it with
  30. %a noun, and then delete the noun from the list so that the
  31. %next noun can be put in the next position we find a @
  32. for ind=1:length(find(rest=='@'))
  33. [tok rest]=strtok(rest,'@');
  34. rest(1)=[];
  35. rest=[tok nouns{1} rest];
  36. nouns(1)=[];
  37. end
  38. %Do the same process above for if the symbol is a # and
  39. %replace the symbol with a verb
  40. elseif str(ind)=='#'
  41. for ind=1:length(find(rest=='#'));
  42. [tok rest]=strtok(rest,'#');
  43. rest(1)=[];
  44. rest=[tok verbs{1} rest];
  45. verbs(1)=[];
  46. end
  47. %Do the same process above for if the symbol is a * and
  48. %replace the symbol with an adjective
  49. elseif str(ind)=='*'
  50. for ind=1:length(find(rest=='*'))
  51. [tok rest]=strtok(rest,'*');
  52. rest(1)=[];
  53. rest=[tok adjectives{1} rest];
  54. adjectives(1)=[];
  55. end
  56. end
  57. end
  58. %Write the line into the new file. The while loop will then move
  59. %onto the next line and do all the same steps. When the line no
  60. %longer has anything, the loop will finish.
  61. fprintf(fh1,rest);
  62. end
  63.  
  64. end
  65. %Close both files that we opened in the beginning
  66. fclose(fh1);
  67. fclose(fh);
  68. end
Add Comment
Please, Sign In to add comment