printing matching lines of text from a text file

3 visualizaciones (últimos 30 días)
Edgar Gonzalez
Edgar Gonzalez el 17 de Oct. de 2018
Comentada: OCDER el 18 de Oct. de 2018

I need to write a code that prints out all matching lines of code that includes the user inputted state and the assigned value giving in the switch state. My output isn't what it's supposed to be. How can I fix this? I attached the text file. Thanks in advanced

% 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
    for long = long_deg
        if ~contains(l,state), continue, end
        fgets(fid)
    end
    for lat = lat_deg
        if ~contains(l,state), continue, end
        fgets(fid)
    end
end

fclose(fid);

Respuesta aceptada

OCDER
OCDER el 17 de Oct. de 2018
This looks like a bad approach. If your input file uses a 3-digit degree, then all your values are ruined.
lat_deg = str2double(l(1:2));
lat_min = str2double(l(4:5));
long_deg = str2double(l(8:9)); %WHAT IF it's a 3-digit degree?
long_min = str2double(l(11:12));
For instance:
72°47N 56°09W Upernavik Greenland Denmark
71°39N 128°52E Tiksi Sakha Republic Russia
1234567890123456789
-- -- -- --
For Russia, str2double(l(8:9)) will NOT get "128".
Here are some pointers. Not sure what you need:
%Consider reading all the file text at once, if it's not too large. This prevent your program from accumulating too many open files (fopen + error leaves a lot of open files).
FID = fopen('cities.txt', 'r');
HDR = regexp(fgetl(FID), '\t', 'split');
FMT = repmat('%s', 1, numel(HDR));
TXT = textscan(FID, FMT, 'delimiter', '\t');
fclose(FID);
Data = [TXT{:}];
%Use sscanf to get the degree, minutes, and NSEW direction
DegMinDir = sscanf(Data{1, 1}, '%d°%d%c');
Deg = DegMinDir(1);
Min = DegMinDir(2);
Dir = char(DegMinDir(3));
%If you want to find all Data for a particular state, use ismember or strcmpi
state = 'Aysén';
StateLoc = strcmpi(Data(:, 4), state);
StateData = Data(StateLoc, :);
%I recommend adding some protection against wrong state names inputted by the user
%such as "alabama" or "Alabama " instead of exactly "Alabama"
switch (lower(strtrim(state)))
case 'alabama'
...
otherwise
error('not a valid state name');
end
  6 comentarios
Edgar Gonzalez
Edgar Gonzalez el 18 de Oct. de 2018
Apologies for the large amount of questions, but I'm having problems stripping my output.
lat =
3×1 cell array
{'43°10N'}
{'42°54N'}
{'40°40N'}
for this output I only need (43,42,40) so I can organize them in order:(40,42,43). I'm trying to use strip() but that only stripped 'N'. How can I strip these of everything else?
OCDER
OCDER el 18 de Oct. de 2018
strip is for removing whitespace, as what help strip returns:
strip Remove leading and trailing whitespaces
To get the first number of lat, you could do this.
lat = {'43°10N';
'42°54N';
'40°40N'};
latDeg = cellfun(@(x) sscanf(x, '%d', 1), lat);
Look up sscanf.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by