Info

This question is closed. Reopen it to edit or answer.

Trying to use dlmread and getting a couple errors, pretty urgent

1 view (last 30 days)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 47, field number 4) ==> + 0.64967 1.00000 1.54 1 1 1 1 1.54\n
I literally just installed a free trial and started using MATLAB. I'm trying to use some data I got from the Human Mortality Database (uploaded a text file). I attached the data to this thread. I'm trying to convert this data into a matrix.
Any ideas? Also, if anyone's willing to get in touch with me and guide me through this entire process of using MATLAB for my project, I'll even go as far as giving you a gift on PayPal.

Answers (1)

Image Analyst
Image Analyst on 22 Feb 2019
Edited: Image Analyst on 22 Feb 2019
I think you're going to have to write a custom reader, because not all lines have the same number of numbers on them. Some lines have "110+ " instead of two numbers.
Use
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
% Now parse textLine and be sure to handle all special/unusual cases.
if contains(textLine, '110+')
% etc.
else
end
end
% All done reading all lines, so close the file.
fclose(fileID);
  1 Comment
Annas Farooq
Annas Farooq on 22 Feb 2019
Alright- just did that, here are two resulting errors:
Error using contains
Search term must be a string array, character vector, or cell array of character vectors.
Error in untitled2 (line 11)
if contains(textLine, '110+')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!