Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

I need help importing this complicated data set into MATLAB.

조회 수: 1 (최근 30일)
Drew L
Drew L 2016년 2월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
I am trying to import a data set that contains the latitude, longitude, and the thickness of the Earth's crust at that coordinate. The difficult part is the formatting of the data within the text file. It is fomatted as follows:
_______________________________________________________
column 1 ; column 2
latitude_1 ; crustal thickness w/ sediment_1
longitude_1 ; crustal thickness w/o sediment_1
latitude_2 ; crustal thickness w/ sediment_2
longitude_2 ; crustal thickness w/o sediment_2
latitude_3 ; crustal thickness w/ sediment_3
longitude_3 ; crustal thickness w/o sediment_3
_______________________________________________________
And it continues like this for a few thousand data points.
I would like to be able to have the latitudes in one column, the longitudes in once column, and the crustal thicknesses in their own columns.
I have tried all sorts of stuff, and have searched through a ton of forum posts, but I have not been able to figure this out.
Here is what I have so far, but this method has not produced usable results. More specifically, the for loops don't always pick out the correct values. Latitude should never be greater than 90, yet the for loop is producing values greater than 100. I dont know how. I wouldnt mind abandoning the for loop method if I can find a way to textscan the data into columns.
fid = fopen('nam-data-xl_2.txt');
N = 3902;
formatSpec = '%s%f%f';
Mat = textscan(fid,formatSpec,N);
fclose(fid);
coord=Mat{3};
thick=Mat{2};
lati=[];
k=1;
for i = 1:2:3902
lati(k)= coord(i);
k=k+1;
end
longi=[];
j = 1;
for ii=2:2:3902
longi(j)=coord(ii);
j=j+1;
end
coordinates = [lati; longi];
crsti=[];
l = 1;
for iii = 2:2:3902
crsti(l) = thick(iii);
l = l+1;
end

답변 (1개)

Kirby Fears
Kirby Fears 2016년 2월 3일
편집: Kirby Fears 2016년 2월 3일
Drew,
I made a text file called DrewL.txt which literally has the faux-data you posted in your question. I am using the format string '%s%s' since the faux-data is semicolon separated strings.
The code below reads the data, separates everything out into 4 columns, and puts them all into 1 table. You should be able to adapt this to your actual data. If you post a more realistic portrayal of your data, I could possibly help make the required adaptations.
formatSpec = '%s%s';
fid = fopen('DrewL.txt');
read = textscan(fid,formatSpec,'Delimiter',';','HeaderLines',1);
fclose(fid);
read = [read{:}];
latitude = read(1:2:end,1);
longitude = read(2:2:end,1);
crustSed = read(1:2:end,2);
crustNoSed = read(2:2:end,2);
data = table(latitude,longitude,crustSed,crustNoSed);
Hope this helps.
  댓글 수: 3
Drew L
Drew L 2016년 2월 3일
This should work, however the latitude and longitude are on top of each other in the same column. So the format is
column 1
lat1
long1
lat2
long2
I need it to be:
column 1
lat 1
lat 2
column 2
long 1
long 2
Thank you
Kirby Fears
Kirby Fears 2016년 2월 4일
I don't understand your last comment. Did you try the code I posted?

제품

Community Treasure Hunt

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

Start Hunting!

Translated by