I need to scan through a textfile line by line and pull out numerical variables corresponding to given line beginnings (e.g. save subjectnumber = 2 for the line 'Subject number: 2').
I am currently attempting to do this by loading the file with fopen, then using fget1 to work through the file 1 line at a time, and comparing the relevant amount of characters at the beginning of each line with saved strings which act as 'keys', using the strncmp function: If the first 'n' characters of the line match the key, the script would then save the numerical value as a variable in the workspace, to later incorporate into the final data structure.
However the strncmp function does not seem to be working correctly, and I cannot figure out why. Regardless of whether I compare Char array to Char array, or convert to Strings before comparison, the function returns a logical '0' even when the key matches the line. I can copy and paste the retrieved line from the document to the command window, use this to test the strncmp function against the key variable saved in the workspace, and get a logical '1' true result. However in the script itself, the function always returns logical '0'.
Has anybody encountered a similar issue before?
fileID = fopen('textfile.txt');subkey = " S u b j e c t :";while ischar(tline) tline = string(fgetl(fileID)) % get next line & convert to string
submatch = strncmp(tline, subkey, length(subkey)) %check match for subject key PROBLEM LINE
if submatch == 1 % code to save numerical variable
endend
The printed output for fgetl for the line containing the desired information, and the subsequent strncmp check is:
tline =
" S u b j e c t : 1 " % i.e. identical to the specified key 'subkey' over the first 16 character
submatch =
logical 0
Best Answer