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!