file handling with coordinates

I have to write a code that organizes a text file filled with coordinates and organize them by latitude or longitude for each state(EX: CA would be North-South and Tennessee would be East-West) and then print out the cities in order by distance for whichever state the user inputted. I have attached the text file. I am not expecting somebody else to do the work for me. I just don't know where to start. Specifically I don't know how I would go about beginning to organize the cities per state.
Any and all help would be greatly appreiciated. Thank you!

2 Kommentare

jonas
jonas am 12 Okt. 2018
I dont really understand the problem you are facing. Can you elaborate?
Edgar Gonzalez
Edgar Gonzalez am 12 Okt. 2018
Bearbeitet: Edgar Gonzalez am 12 Okt. 2018
I attached a screeshot of the assignment maybe that can help. This is the code I have as of now. A lot of it is probably wrong. Right now I was just attempting to get the correct syntax down
% 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'
fprintf('hi')
end
end
fclose(fid)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

jonas
jonas am 12 Okt. 2018
Bearbeitet: jonas am 12 Okt. 2018

0 Stimmen

I would approach this roughly as follows:
  • Read all coordinates and group them by state
  • Use a grouping function ( findrows/ splitapply, varfun, grpstats) to calculate the max/min long/lat in each state (4 values per state
  • If you have the mapping toolbox, calculate the east-west and north-south arclength to find the longest distance.
  • Use a grouping function again to output a sorted list of cities per state that is returned to the user upon request.
Try yourself, and ask if you have trouble with one of the steps.

3 Kommentare

Edgar Gonzalez
Edgar Gonzalez am 15 Okt. 2018
Bearbeitet: Walter Roberson am 15 Okt. 2018

This is what I decide to try. I want to print all lines of the text file that contain both the state and the assigned lat or long value given in the switch case. I know lgets works for when you want to find 1 matching value. How can I write it to work for both? (ex: find all lines in the file that contain California and a longitude of 120)

% 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
    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
    l=fgets(fid);
    if ~contains(l,state), continue, end % keep going until found
    lat = str2double(l(1:2)); % defines variables for coordinates
    lat_min = str2double(l(4:5));
    long = str2double(l(8:9)); 
    long_min = str2double(l(11:12));
  end
fclose(fid);
Walter Roberson
Walter Roberson am 15 Okt. 2018
Have you considered creating a vector of target lat or long values, and a cell array of character vectors with the names of the states? And use ismember() to match the input state to the name list, getting out an index (second output of ismember) that you use to index the lat or long array?
I do not know what those lat or long values are doing for you? And I am concerned because you have not defined both of them for each state, which always leads to the risk that you could accidentally use a value appropriate for the previous state.
jonas
jonas am 15 Okt. 2018
That does not look optimal. Where did you get those coordinates from? Is it the "length" of the state in units of degrees? Do you know that you cannot compare latitude and longitude in absolute terms? You should compare arclength if you want to calculate the "length" of the state.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 12 Okt. 2018

Kommentiert:

am 15 Okt. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by