Script to load .mat file from a path, calculate a value based on a formula, loop the same until the last file of the folder (could be 1000s) and add the calc'd values in one.

조회 수: 4 (최근 30일)
I have a folder with thousands of .mat files.
I need some help to write a script that will go in the folder, pick up the first file, choose specific arrays tp build my formula for calculating an X value, then looping the same process again, and adding all the X values from each loop until the last file of the folder.

답변 (1개)

Kautuk Raj
Kautuk Raj 2023년 7월 4일
This is a sample script which will help you accomplish what you need:
% Specify the folder containing the .mat files
folder = 'path/to/folder';
% Get a list of all .mat files in the folder
files = dir(fullfile(folder, '*.mat'));
% Initialize an empty array to store the X values
X_all = [];
% Loop through each file in the folder
for i = 1:numel(files)
% Load the current file
file = fullfile(folder, files(i).name);
data = load(file);
% Extract the arrays needed for the X calculation
array1 = data.array1; % replace with actual variable name
array2 = data.array2; % replace with actual variable name
% Calculate the X value from the arrays
X = your_formula(array1, array2); % replace with your actual formula
% Add the X value to the array of all X values
X_all = [X_all; X];
end
% Sum all the X values
X_sum = sum(X_all);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by