substract and make answer to write as an output file
1 次查看(过去 30 天)
显示 更早的评论
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")
采纳的回答
Ayush
2024-1-17
Hi @幸一
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!