Info

This question is closed. Reopen it to edit or answer.

How to print all lines from text file that match the input

1 view (last 30 days)

I need to print all lines from the .txt file that match the user inputted state plus the assigned lat or long value (its different per state & and only one per state). Where do I go from here? I attached the text file. Any suggestions?

My code:

    % Project 1
fid = fopen('cities.txt', 'r'); % opens the file with read permission
state = input('Which state are you visiting ','s');
found = false;
while ~found && ~feof(fid) % tests for end of file
    l=fgets(fid);   
    if ~contains(l,state), continue, end % keep going until found
    lat_deg = str2double(l(1:2)); % defines variables for coordinates
    lat_min = str2double(l(4:5));
    long_deg = str2double(l(8:9)); 
    long_min = str2double(l(11:12));
    switch state
        case 'Alabama'
            long = 87;
        case 'Alaska' 
            long = 152;
        case 'Arizona'
            long = 111;
        case 'California'
            long = 120;
        case 'Colorado'
            lat = 39;
        case 'Connecticut'
            lat = 42;
        case 'Delaware'
            long = 76;
        case 'Florida'
            long = 82;
        case 'Georgia'
            long = 84;
        case 'Hawaii'
            lat = 21;
        case 'Idaho'
            long = 114;
        case 'Illinois'
            long = 89;
        case 'Indiana'
            long = 86;
        case 'Iowa'
            lat = 42;
        case 'Kansas'
            lat = 39;
        case 'Kentucky'
            lat = 38;
        case 'Louisiana'
            long = 92;
        case 'Maine'
            long = 69;
        case 'Maryland'
            long = 77;
        case 'Massachusetts'
            lat = 42;
        case 'Michigan'
            long = 85;
        case 'Missouri'
            long = 92;
        case 'Montana'
            lat = 47;
        case 'Nebraska'
            lat = 41;
        case 'Nevada'
            long = 117;
        case 'New Hamphsire'
            long = 72;
        case 'New Jersey'
            long = 75;
        case 'New Mexico'
            long = 106;
        case 'New York'
            lat = 42;
        case 'North Carolina'
            lat = 36;
        case 'North Dakota'
            lat = 48;
        case 'Ohio'
            lat = 40;
        case 'Oklahoma'
            lat = 36;
        case 'Oregon'
            lat = 45;
        case 'Pennsylvania'
            lat = 41;
        case 'Rhode Island'
            long = 72;
        case 'South Carolina'
            lat = 34;
        case 'South Dakota'
            lat = 44;
        case 'Tennessee'
            lat = 36;
        case 'Texas'
            lat = 31;
        case 'Utah'
            long = 112;
        case 'Vermont'
            long = 73;
        case 'Washington'
            lat = 47;
        case 'West Virginia'
            long = 81;
        case 'Wisconsin'
            long = 90;
        case 'Wyoming'
            lat = 43;
    end
end

fclose(fid);

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!