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

  • Context: MATLAB 
  • Thread starter Thread starter sami-rehman
  • Start date Start date
  • Tags Tags
    Extraction Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 5K views
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!