printing matching lines of text from a text file
Ältere Kommentare anzeigen
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
endfclose(fid);
1 Kommentar
Image Analyst
am 17 Okt. 2018
And what is it supposed to be?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu String Parsing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!