Filter löschen
Filter löschen

How to write the results of createFit to a csv or txt file

15 Ansichten (letzte 30 Tage)
Bradley
Bradley am 14 Mär. 2024
Kommentiert: Voss am 14 Mär. 2024
Ive been working on a way to process of huge amount of single beam bathymetry data, I have roughly 40000 files from all sorts of different environments that need to be processed and put into a different bathymetry program (EIVA) and will eventually be exported as a geotiff for use in GIS. I need the final output of matlab to be a text file or csv in order to properly import it in my bathymetry program. The below code is what ive been writing but the last step is to export the fitted plane as a csv or text file, in this case im exporting as a text file but it actually doesnt matter.
My struggle is that the result of my fitted plane is not a matrix, so I think what I need to do is declare 3 more variables after the fitted plane has been calculated. Right now the values that im writing to my matrix (line 105) are the prepared surface data values, however when the plane is fitted to these values there is a slight change from the lowess smoothing. How would I write the results of the smoothing to three new values and finally to a matrix? Thanks again!
clear
% Path to where the CSV files are saved
P = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222';
% Path to where the TXT files will be saved
Q = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222/Corrected CSV';
% Define what file types you want to process, in this case .csv files
S = dir(fullfile(P,'*.csv'));
% Sort files by name
S = natsortfiles(S);
%%/////////////////////////////////////////////////////////////////////////
%% For loop include all .csv files in the parent folder, will continue to
%% process files until all files have been read.
%%/////////////////////////////////////////////////////////////////////////
for k= 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
L = readmatrix(F);
[M, TFrm, TFoutlier] = rmoutliers(L,1);
lat = M(:,1);
%Longitude
lon = M(:,2);
%Depth
dep = M(:,3);
%Altitude
alt = M(:,4);
Dep = alt+dep;
%smoothed true depth
depS = smoothdata(Dep,'movmedian',500);
%%/////////////////////////////////////////////////////////////////////
%% Plots raw depth and smoothed depth
%%/////////////////////////////////////////////////////////////////////
% figure
% plot(Dep)
% hold on
%
% plot(depS)
% title('depth and smoothed depth')
% legend({'depth', 'smoothed dep'}, 'Location','northeast')
% hold off
%%/////////////////////////////////////////////////////////////////////
%% Plots a fitted surface to the points, for all bottom types
%%/////////////////////////////////////////////////////////////////////
% figure
[xData, yData, zData] = prepareSurfaceData( lon, lat, depS );
% Set up fittype and options.
ft = fittype( 'lowess' );
% ft = 'cubicinterp';
% opts = fitoptions( 'Method', 'CubicSplineInterpolant' );
% opts.ExtrapolationMethod = 'none';
% opts.Normalize = 'on';
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% [fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'untitled fit 1', 'depS vs. lon, lat', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'lon', 'Interpreter', 'none' );
ylabel( 'lat', 'Interpreter', 'none' );
zlabel( 'depS', 'Interpreter', 'none' );
grid on
view(30,65);
%%/////////////////////////////////////////////////////////////////////
%% Plots the uncorrected bathymetry with normal plane
%%/////////////////////////////////////////////////////////////////////
% figure
% plot3(lat, lon, depS, '.')
% hold on
% grid on
% view(35,60)
% title(sprintf("File: %s", F))
% downsampling = 100; % to reduce risk of out of memory" in affine_fit
% ind = (1:downsampling:numel(lat));
% XYZ = [lat(ind) lon(ind) depS(ind)];
% [n_1,~,p_1] = affine_fit(XYZ);
%
% %plot the two adjusted planes
% [Xf,Yf] = meshgrid(lat(ind),lon(ind));
%
% %fitted plane
% Zf = - (n_1(1)/n_1(3)*Xf+n_1(2)/n_1(3)*Yf-dot(n_1,p_1)/n_1(3));
% surf(Xf,Yf,Zf,'edgecolor','none','facecolor','red','facealpha',0.25);
%%/////////////////////////////////////////////////////////////////////
%% Creates a figure with corrected points to flat plane, for
%% for non-complex, smooth bottom environments
%%/////////////////////////////////////////////////////////////////////
% figure
% depf = - (n_1(1)/n_1(3)*lat+n_1(2)/n_1(3)*lon-dot(n_1,p_1)/n_1(3));
% % plot3(lat, lon, depf, '.')
% hold on
% grid on
% view(35,60)
% % title('Average Surface')
%%/////////////////////////////////////////////////////////////////////
%% Write values to a matrix for import into NaviModel
%%/////////////////////////////////////////////////////////////////////
X = [xData(:), yData(:), zData(:)];
[~,F,~] = fileparts(X);
F = sprintf('%s.txt',F);
writematrix(X,fullfile(Q,F))
end
%%/////////////////////////////////////////////////////////////////////////
%% Move all processed files to a clean folder
%%/////////////////////////////////////////////////////////////////////////
% movefile '*.csv' 'Cleaned'

Akzeptierte Antwort

Voss
Voss am 14 Mär. 2024
Replace this:
X = [xData(:), yData(:), zData(:)];
[~,F,~] = fileparts(X);
with this:
X = [xData(:), yData(:), fitresult(xData(:), yData(:))];
[~,F,~] = fileparts(F);
  2 Kommentare
Bradley
Bradley am 14 Mär. 2024
I knew that would go somewhere, I kept putting it either in the wrong spot or the syntax was off. Your solution worked like a charm! Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by