my file is so heavy for running, is it possible to divide it to some parts and load m files one after each other?
조회 수: 1 (최근 30일)
이전 댓글 표시
my file is so heavy for running, is it possible to divide it to some parts and load m files one after each other?
댓글 수: 2
John D'Errico
2022년 5월 17일
편집: John D'Errico
2022년 5월 17일
IS IT POSSIBLE? Yes. But why do you think that would help? And what does heavy mean anyway? Perhaps you mean slow? Splitting it up into consecutive pieces will not help in that respect. Or does heavy mean it uses too much memory? It is likely the solution there would be just using better, more memory efficient coding practices.
답변 (1개)
Ravi
2023년 12월 29일
Hi Mohammad Sadegh Nasirianfar,
I understand that you are facing an issue in separating the code into fragments and executing on the data.
Let us assume the main file contains three methods “A”, “B”, and “C”. Create one separate file for each of the methods and name them “A.m”, “B.m”, and “C.m”. Please make sure that new files are present in the same working directory as the main file. In the main file, write a script to execute the methods one after the other. You can also implement a interactive method if you want to terminate the execution at any point after the execution of a method.
Sample main file:
data = A(data);
% print if any diagnostics are required
inp = input('do you want to continue the execution with next method?(1/0)');
if inp == 0:
% exit
end
data = B(data);
% print if any diagnostics are required
inp = input('do you want to continue the execution with next method?(1/0)');
if inp == 0:
% exit
end
data = C(data);
% print if any diagnostics are required
inp = input('do you want to continue the execution with next method?(1/0)');
if inp == 0:
% exit
end
In case if you want to divide your data into separate files rather than dividing the methods, you can follow the below approach.
The following code assumes that there is a data file named, “original_data_file.mat” that contains a field, “data” that contains 100 entries. The objective is to divide that into 10 files each with 10 entries.
% Load the data from the original .mat file
original_data = load('original_data_file.mat');
field_names = fieldnames(original_data);
% Assuming the data is stored in a field named 'data'
% and it is a numerical vector with 100 entries.
data = original_data.(field_names{1});
% Number of entries per new file
entries_per_file = 10;
% Divide and save the data into 10 new .mat files
for i = 1:10
start_index = (i - 1) * entries_per_file + 1;
end_index = i * entries_per_file;
% Extract the subset of data for the current file
subset_data = data(start_index:end_index);
% Generate a new file name
new_file_name = sprintf('data_part_%d.mat', i);
% Save the subset of data into the new .mat file
save(new_file_name, 'subset_data');
end
disp('The original data has been divided and saved into 10 new .mat files.');
I hope this solution works for you.
Thanks,
Ravi Chandra
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!