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
Click For Summary
SUMMARY

The forum discussion focuses on extracting city names from a text file using MATLAB for an Automatic Mail Sorting Machine project. The user is utilizing the textscan function to read data from a file named "string.txt" but encounters errors when attempting to skip lines. The recommended solution involves modifying the textscan command to treat each line as a string, ensuring that the city name is correctly captured regardless of its position in the address. Additionally, the use of str2num is suggested for handling numerical data in the file.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of the textscan function in MATLAB
  • Knowledge of handling strings and character arrays in MATLAB
  • Basic understanding of file I/O operations in MATLAB
NEXT STEPS
  • Learn how to use the textscan function effectively for reading mixed data types
  • Explore the str2num function for converting string representations of numbers to numeric values
  • Research MATLAB's file I/O functions for better handling of text files
  • Investigate regular expressions in MATLAB for advanced text parsing techniques
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and engineers working on projects involving text data extraction and processing, particularly in the context of automated systems like mail sorting machines.

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!
 

Similar threads

Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
6
Views
3K