How to load data with segments

3 views (last 30 days)
JZ
JZ on 21 Sep 2021
Commented: Mathieu NOE on 22 Sep 2021
Hi there,
I have a file, looks like below, it's Lon/Lat/Depth separated by ">". I tried to use importdata(), but only got first part. I need to plot some contour lines using geoplot(). Your suggestions will be appreciated.
> -660 contour -Z-660
288.9812 -7.0500 -660.0000
288.9884 -7.1000 -660.0000
288.9958 -7.1500 -660.0000
289.0000 -7.1777 -660.0000
> -660 contour -Z-660
289.0364 -7.4000 -660.0000
289.0454 -7.4500 -660.0000
289.0500 -7.4748 -660.0000
> -660 contour -Z-660
289.0741 -7.6000 -660.0000
289.0843 -7.6500 -660.0000
289.0948 -7.7000 -660.0000
289.1000 -7.7240 -660.0000
289.1056 -7.7500 -660.0000
289.1166 -7.8000 -660.0000

Accepted Answer

Mathieu NOE
Mathieu NOE on 21 Sep 2021
hello
I copied / pasted your data in a txt file
try this :
%% read one file
clc
clearvars
filename = 'mydata.txt';
data = myreadfunction(filename)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data = myreadfunction(filename)
a = readlines(filename);
[m,~] = size(a);
data = [];
% find / extract the different sets of data - separated by text "> -660 contour -Z-660"
start_indexes = find(contains(a,'>'))+1;
stop_indexes = [start_indexes(2:end)-2; m];
for ci = 1:length(start_indexes)
tmp = str2num(char(a(start_indexes(ci):stop_indexes(ci)))); % one segment of data
data = [data; tmp];
end
end
it gives :
data =
288.9812 -7.0500 -660.0000
288.9884 -7.1000 -660.0000
288.9958 -7.1500 -660.0000
289.0000 -7.1777 -660.0000
289.0364 -7.4000 -660.0000
289.0454 -7.4500 -660.0000
289.0500 -7.4748 -660.0000
289.0741 -7.6000 -660.0000
289.0843 -7.6500 -660.0000
289.0948 -7.7000 -660.0000
289.1000 -7.7240 -660.0000
289.1056 -7.7500 -660.0000
289.1166 -7.8000 -660.0000
  2 Comments
JZ
JZ on 22 Sep 2021
That's brilliant! Thank you so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!