Clear Filters
Clear Filters

Slow array of structure updating in saving variables in app designer

36 views (last 30 days)
I am working on developing an application in app designer for doing a bunch of fits and saving the results in an array of structures, where each structure has various types of fields about the fit results. When I save the file, I update the array with the fit_info structure each fit, and append to the array of structures for each save step. I eventually will be doing 89x13 fits. But, each cycle gets slower and slower, so it is impractically slow by about 30 fits. I looked into saving a separate variable for each fit, by dynamically naming the variables, i.e., fit_info_1_1, fit_info_1_2, etc., but I know this is bad programming practice. Does anyone have any suggestions?
function SaveFitButtonPushed(app, event)
data_source=string(app.TextArea.Value);
filename=strcat("fit_results_",data_source);
if isfile(filename)
matObj=matfile(filename,'Writable',true);
matObj.fit_info_a(app.ColumnEditField.Value,app.CompoundEditField.Value)=app.fit_info_1;
else
fit_info_a(app.ColumnEditField.Value,app.CompoundEditField.Value)=app.fit_info_1;
save (filename,'fit_info_a','-v7.3');
end
if ~isempty(app.fit_info_2.pm95)
data_source=string(app.TextArea.Value);
filename=strcat("fit_results_2_",data_source);
if isfile(filename)
matObj=matfile(filename,'Writable',true);
matObj.fit_info_b(app.ColumnEditField.Value,app.CompoundEditField.Value)=app.fit_info_2;
else
fit_info_b(app.ColumnEditField.Value,app.CompoundEditField.Value)=app.fit_info_2;
save (filename,'fit_info_b','-v7.3');
end
end
app.k_refS_1refS_2refTextArea.Value='';
app.s_ETextArea.Value='';
app.k_refS_1refS_2refTextArea_2.Value='';
app.s_ETextArea_2.Value='';
app.phi_refEditField.Value=30;
app.phi_refEditField_2.Value=30;
str='[]';
app.OmitPointsEditField.Value=str;
app.OmitPointsEditField_2.Value=str;
end

Answers (1)

Aditya
Aditya on 26 Jun 2024 at 6:13
Edited: Aditya on 26 Jun 2024 at 6:14
Hi Sarah,
Here are some suggestions that you can try to improve the performance:
1) Preallocate the Structure Array: Preallocate 'fit_info_a' and 'fit_info_b' arrays using 'repmat(struct(), 89, 13)' to avoid dynamic resizing.
2) Use 'matfile' Efficiently: Open the MAT-file once and update the specific elements directly.
Here is the sample code based on the above suggestion:
% Check if the file exists and create or load it accordingly
if isfile(filename)
matObj = matfile(filename, 'Writable', true);
else
fit_info_a = repmat(struct(), 89, 13); % Preallocate for 89x13 fits
save(filename, 'fit_info_a', '-v7.3');
matObj = matfile(filename, 'Writable', true);
end
% Update the specific element in the preallocated structure array
matObj.fit_info_a(app.ColumnEditField.Value, app.CompoundEditField.Value) = app.fit_info_1;
similarly, you can modify the other section of the code.
By preallocating the array and using the 'matfile' object, you avoid the overhead of dynamically resizing the array and loading/saving the entire file repeatedly. This should improve the performance of your application.
I hope the above suggestions will help you improve the performance!

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!