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 view (last 30 days)
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 Comments
John D'Errico
John D'Errico on 17 May 2022
Edited: John D'Errico on 17 May 2022
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.
Mohammad Sadegh Nasirianfar
I want to analyze a geotechnical test with different methods. if I want to run it for all in one m.file it takes a lot of time. I wwanted to run one method, then another and so forth. but I do not know how can I run. when I write for example load sbtype.m it gives me an error.

Sign in to comment.

Answers (1)

Ravi
Ravi on 29 Dec 2023
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

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!