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

In summary, the conversation was about a final year project involving extracting city names from a text file using MATLAB. The project was an Automatic Mail Sorting Machine and the code used to address this issue was discussed. Suggestions were given for how to treat numerical data and strings within the text file. The importance of providing sample data and specifying errors was also mentioned.
  • #1
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
  • #2
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!
 

1. How can I extract city names from a text file in MATLAB?

To extract city names from a text file in MATLAB, you can use the built-in function "regexp" which allows you to search for specific patterns in a string. You can also use the "strsplit" function to split a string into an array based on a delimiter, such as a comma or space, and then extract the city name from the resulting array.

2. Can MATLAB automatically sort mail based on city names extracted from a text file?

Yes, MATLAB has powerful built-in functions such as "sort" and "unique" that can sort and organize data based on specific criteria, such as city names. By extracting city names from a text file, you can use these functions to automatically sort and group mail based on their destination city.

3. What are some tips for accurately extracting city names from a text file in MATLAB?

One tip is to ensure that your text file is formatted consistently, with city names appearing in the same format (e.g. "New York City" vs "NYC"). You should also consider using regular expressions to handle variations in city names (e.g. "St. Louis" vs "Saint Louis"). Additionally, you can use the "lower" function to convert all text to lowercase, making it easier to search for specific patterns.

4. Can I extract city names from a text file that contains other types of information?

Yes, it is possible to extract city names from a text file that also contains other types of information, such as names or addresses. However, you may need to use more advanced techniques, such as pattern matching or machine learning algorithms, to accurately identify and extract city names from these types of files.

5. Are there any limitations to using MATLAB for automatic mail sorting based on city names?

While MATLAB is a powerful tool for data analysis and manipulation, it may not be the most efficient or practical solution for large-scale mail sorting operations. It is important to consider the size and complexity of the data, as well as the speed and accuracy requirements, when deciding whether MATLAB is the best tool for the job.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top