フィルターのクリア

substract and make answer to write as an output file

1 回表示 (過去 30 日間)
幸一
幸一 2024 年 1 月 17 日
コメント済み: 幸一 2024 年 1 月 18 日
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 件のコメント
幸一
幸一 2024 年 1 月 17 日
please check the attached files to this thread
幸一
幸一 2024 年 1 月 18 日
hello matlab community.
Anybody know the answer for this please?

サインインしてコメントする。

採用された回答

Ayush
Ayush 2024 年 1 月 17 日
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 件のコメント
幸一
幸一 2024 年 1 月 17 日
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.
幸一
幸一 2024 年 1 月 18 日
hello sir,
I tried your suggesstion, but unfortunately it is not working.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by