substract and make answer to write as an output file

1 view (last 30 days)
Please anyone who can understand to this problem let me know the solution.
Your kind support is highly appreciated
all these files are 200x200 matrices.
fdir1 = 'depth'; % this is the initial file.
I want to substract above file from all the other depth files 'depth_00001' to 'depth_03600' and write the output as 'slide_00001' to 'slide_03600'.
fdir1 = 'D:/Mejena_Case/NHWAVE_DeformSlide_ReducedArea/results_orig/';
fdir2 = 'D:/Mejena_Case/NHWAVE_DeformSlide_ReducedArea/results_orig/';
ini_depth_file = fdir1;
% load the initial depth file
dep_init = load([ini_depth_file 'depth']);
% given name for the depth file want to read
slide_dep_file = fdir2;
% load the required depth file
depth_data = load([slide_dep_file 'depth_00139']);
% read the the load files in fdir1 and fdir2
dep_init = readmatrix([ini_depth_file 'depth']);
depth_data = readmatrix([slide_dep_file 'depth_00139']);
% operation function
slide = (depth_data-dep_init);
% save the output file in to the same directory
writematrix(slide, "slide_00139.txt", "Delimiter",',');
type("slide_00139.txt")
  5 Comments
幸一
幸一 on 18 Jan 2024
hello matlab community.
Anybody know the answer for this please?

Sign in to comment.

Accepted Answer

Ayush
Ayush on 17 Jan 2024
You can use a loop to iterate through the file names, load the matrices, perform the subtraction, and then save the result. Here's a code snippet that should accomplish what you described:
% Define the directory where the files are located
directory = './'; % Adjust this if your files are in a different directory
% Load the initial file
initial_file = 'depth.mat';
initial_data = load(fullfile(directory, initial_file));
initial_matrix = initial_data.depth; % Assuming the matrix is saved under the variable 'depth'
% Iterate through the depth files and perform the subtraction
for i = 1:3600
% Generate the file names
depth_file_name = sprintf('depth_%05d.mat', i);
slide_file_name = sprintf('slide_%05d.mat', i);
% Load the current depth file
depth_data = load(fullfile(directory, depth_file_name));
depth_matrix = depth_data.depth; % Assuming the matrix is saved under the variable 'depth'
% Subtract the initial matrix from the current depth matrix
slide_matrix = depth_matrix - initial_matrix;
% Save the result to a new file
save(fullfile(directory, slide_file_name), 'slide_matrix');
end
% Note: The above code assumes that the matrix variable inside each .mat file is named 'depth'.
% If the variable has a different name, adjust the code accordingly.
Thanks,
Ayush
  2 Comments
幸一
幸一 on 17 Jan 2024
thank you very much sir.
great help you gave me to save my time.
Because, I am very new to matlab. Previously I never used this program.
幸一
幸一 on 18 Jan 2024
hello sir,
I tried your suggesstion, but unfortunately it is not working.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!