
How to extract and save water depth data from a 2dm file?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
영택 조
el 9 de Nov. de 2023
Editada: Angelo Yeo
el 10 de Nov. de 2023
Do you know about a program called 'Surface-water Modeling System (SMS)'? I am doing homework to create a 'homework_depth.dat' file using the 'homework.2dm' file created by the SMS program to meet the conditions described later. Currently, I created the code as below, but the depth data is not recorded in the 'homework_depth.dat' file. It seems that depth data extraction is not working properly with this code alone. How should I modify the code below to make it work properly? I am uploading the work I have done so far.
The conditions are as follows:
Deepest: obc (far left of grid in .2dm file)
Shallowest: Freshwater input boundary (far right of grid in .2dm file)
Deepest water depth: 50m
Shallowest water depth: 30m
Constant decrease only in the X-axis direction
Scatter interval: 100m each on the x-axis, 50m each on the y-axis
%% Code
clear all; clc; close all;
pwd;
addpath('./01__Data')
conf.base = fullfile(pwd, '01__Data', 'make_input');
filePath = fullfile(conf.base, 'homework.2dm');
outputFilePath = fullfile(pwd, '01__Data', 'homework_depth.dat');
fileID = fopen(filePath, 'r');
data = textscan(fileID, '%f %f %f', 'HeaderLines', 1);
fclose(fileID);
depth = data{3};
minDepth = 30;
maxDepth = 50;
xGrid = data{1};
yGrid = data{2};
depth(xGrid == min(xGrid)) = maxDepth;
depth(xGrid == max(xGrid)) = minDepth;
xInterval = 100;
yInterval = 50;
[~, index] = sort(xGrid, 'descend');
depthSorted = depth(index);
fileID_dat = fopen(outputFilePath, 'w');
for i = 1:length(depthSorted)
fprintf(fileID_dat, '%f\n', depthSorted(i));
end
fclose(fileID_dat);
0 comentarios
Respuesta aceptada
Angelo Yeo
el 10 de Nov. de 2023
Editada: Angelo Yeo
el 10 de Nov. de 2023
You may have an issue with importing data. "fopen" and "textscan" are low-level functions, and it may be hard to get them to use properly.
Why don't you try readtable? It offers numerous options and can potentially import the data as per your requirements. Give the code below a shot to see if it accomplishes the desired data import:
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 6);
% Specify range and delimiter
opts.DataLines = [3, Inf];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["MESH2D", "VarName2", "VarName3", "VarName4", "VarName5", "Var6"];
opts.SelectedVariableNames = ["MESH2D", "VarName2", "VarName3", "VarName4", "VarName5"];
opts.VariableTypes = ["string", "double", "double", "double", "double", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
% Specify variable properties
opts = setvaropts(opts, ["MESH2D", "Var6"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["MESH2D", "Var6"], "EmptyFieldRule", "auto");
% Import the data
homework = readtable("homework.txt", opts); % You may change it to 2dm file you work with.
head(homework)
My last comment is: utilizing the "Import Data" feature in MATLAB is worth mentioning as it holds significant power.

0 comentarios
Más respuestas (1)
Kyoung Moon Lee
el 9 de Nov. de 2023
Editada: Kyoung Moon Lee
el 9 de Nov. de 2023
use meshgrid
In my opinion, next time don't post the original homework file as is.
% enther lon, lat, depth
x = [];
y = [];
z = [];
% make grid
[x y] = meshgird(x,y);
% save file
writematrix([x(:),y(:),z(:)],'homework_depth.dat')
Ver también
Categorías
Más información sobre Low-Level File I/O en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!