Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function realMadLib(file,struct)
- %Open the file
- fh=fopen(file,'r');
- %Edit the name of the file that we will be writing to
- [file1 edited]=strtok(file,'.');
- newfilename=[file1 '_edited.txt'];
- %Open a new file that's titled the way we need it to be
- fh1=fopen(newfilename,'w');
- %Initialize the empty string so that the while loop can start
- str='';
- %Make a cell array of nouns, verbs and adjectives so they can be indexed
- %out
- nouns={struct.noun};
- verbs={struct.verb};
- adjectives={struct.adj};
- %start the generic loop used for low level file I/O, let it run until the
- %string is not of type char anymore
- while ischar(str)
- str=fgets(fh);
- %Read the first line of the file, the loop will then read the second,
- %third, etc.
- rest=str;
- %If the line contains characters, run another loop
- if ischar(str)
- for ind=1:length(str)
- %Check the string, letter by letter to see if it is one of the
- %symbols we are using
- if str(ind)=='@'
- %If the character is a @, take it out and replace it with
- %a noun, and then delete the noun from the list so that the
- %next noun can be put in the next position we find a @
- for ind=1:length(find(rest=='@'))
- [tok rest]=strtok(rest,'@');
- rest(1)=[];
- rest=[tok nouns{1} rest];
- nouns(1)=[];
- end
- %Do the same process above for if the symbol is a # and
- %replace the symbol with a verb
- elseif str(ind)=='#'
- for ind=1:length(find(rest=='#'));
- [tok rest]=strtok(rest,'#');
- rest(1)=[];
- rest=[tok verbs{1} rest];
- verbs(1)=[];
- end
- %Do the same process above for if the symbol is a * and
- %replace the symbol with an adjective
- elseif str(ind)=='*'
- for ind=1:length(find(rest=='*'))
- [tok rest]=strtok(rest,'*');
- rest(1)=[];
- rest=[tok adjectives{1} rest];
- adjectives(1)=[];
- end
- end
- end
- %Write the line into the new file. The while loop will then move
- %onto the next line and do all the same steps. When the line no
- %longer has anything, the loop will finish.
- fprintf(fh1,rest);
- end
- end
- %Close both files that we opened in the beginning
- fclose(fh1);
- fclose(fh);
- end
Add Comment
Please, Sign In to add comment