Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab word extraction

  1. Apr 28, 2010 #1
    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 thats 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);

    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 ............
  2. jcsd
  3. Apr 30, 2010 #2


    User Avatar
    Science Advisor

    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:

    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:

    Good luck!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook