MATLAB Extracting City Name from a Text File in MATLAB: Tips for Automatic Mail Sorting

  • Thread starter Thread starter sami-rehman
  • Start date Start date
  • Tags Tags
    Extraction Matlab
AI Thread Summary
The discussion focuses on extracting city names from addresses in a text file using MATLAB for an automatic mail sorting project. The user is struggling with the textscan function, which is designed to read formatted data from text files but is encountering errors when trying to skip lines. Suggestions include treating each line as a string to avoid format issues and using str2num for numerical data. The importance of providing sample data and specifying errors for better assistance is also highlighted. The conversation emphasizes the need for proper formatting and understanding of the textscan function to achieve the desired outcome.
sami-rehman
In the backdrop of my final year project i have a MATLAB question to ask you. Suppose we have text file placed in the
workspace of matlab. In the text file is written the address of a person. From that address i have to extract the city
name that is supposed to be writtent in the end. I can extract the city name if it is writtent right at the top of the
address but that's not how actual addresses are written.

My project is Automatic Mail Sorting Machine, after extracting the city name letters would be sorted out...

I am using the following code to address this apparamtly small issue :
%load ('string')
fid = fopen('string.txt')
%fseek(fid, 1, 'eof');

fseek(fid, 0,'eof');
C = textscan(fid,'%n %n% s',1);
%c=fscanf(fid,'%e',[4,inf]);
city=C{:}
%sam=cell2mat(city);
fclose(fid);
%display(sam)

textscan command is of importance here...if city name is written in the third line than the code must skip to the third line
but it yeilds error...text file name is "string"

please help ...
 
Physics news on Phys.org
Welcome to PhysicsForums!

In the future, I'd suggest asking these sorts of programmatic questions in the Programming or Math & Science subsection of the Computing & Technology Forum. That or in the Engineering and CompSci subsection of the Homework Forum.

For this type of question, I'd also include sample data and specify the error that MATLAB gives you.

However, it looks as if your code snippet is a work in progress and that you've tried various things, based on the lines of code commented out. In any case, it looks as if you're trying to read two floating point numbers and then a string on the same row/line! That's the thing with the textscan function: it reads each row of (identically formatted) data into a cell array:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html

What I'd suggest you do is have textscan treat each line as a string %s (or character array %c, especially if there's a space or tab in the text data--see the section on delimiters in the link above, otherwise you might have 'New York City' truncated simply as 'New'. Now, if the first two lines contain numerical data, you can use the str2num command to treat these as numbers:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/str2num.html

Good luck!
 
Back
Top