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)
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);
for i = 1:2:3902
lati(k)= coord(i);
j = 1;
for ii=2:2:3902
coordinates = [lati; longi];
l = 1;
for iii = 2:2:3902
crsti(l) = thick(iii);
l = l+1;

Answers (1)

Kirby Fears
Kirby Fears on 3 Feb 2016
Edited: Kirby Fears on 3 Feb 2016
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);
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.
Kirby Fears
Kirby Fears on 4 Feb 2016
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