Info

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

I need help importing this complicated data set into MATLAB.

1 view (last 30 days)
Drew L
Drew L on 3 Feb 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (1)

Kirby Fears
Kirby Fears on 3 Feb 2016
Edited: Kirby Fears on 3 Feb 2016
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 Comments
Kirby Fears
Kirby Fears on 4 Feb 2016
I don't understand your last comment. Did you try the code I posted?

Products

Community Treasure Hunt

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

Start Hunting!