How to save "optimoptions" to a file?

Is there a way to save "optimoptions" to a txt file? I tried the code below but it is not working. I also tried "dlmwrite" and "writetable". Thanks for the help.
fid = fopen('GA_Results.txt','wt+');
fprintf(fid,'%s', GA_Opts);
fclose(fid);

 採用された回答

Star Strider
Star Strider 2020 年 9 月 14 日

2 投票

I have never tried that.
I would save it to a .mat file. Then load it when you need it.

4 件のコメント

MB
MB 2020 年 9 月 14 日
編集済み: MB 2020 年 9 月 14 日
Thanks for the answer. I'm actually saving the optimization results to "GA_Results" txt file using "writematrix" command. That's why I wanted to save the optimoptions to the same txt file. I think I can save both the results and optimoptions using "save" command to a mat file.
Star Strider
Star Strider 2020 年 9 月 14 日
As always, my pleasure!
When I tried this:
PopSz = 500;
Parms = 6;
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
Q1 = sprintf('%s', opts)
the result was:
Error using sprintf
Unable to convert 'optim.options.GaOptions' value to 'char' or 'string'.
With that in mind, I opted to suggest:
save('Test_Options_Save.mat', 'opts')
then with:
Q2 = load('Test_Options_Save.mat')
the result was:
Q2 =
struct with fields:
opts: [1×1 optim.options.GaOptions]
to be certain that it worked.
It does.
MB
MB 2020 年 9 月 14 日
編集済み: MB 2020 年 9 月 14 日
That's exactly the same error I got but using "save" command and saving to a mat file works perfectly. You can save both variables (GA results and optimoptions) in one call. There is also an example with txt files in the document page, it is working but it is impossible to read the txt file.
save('GA_Results.mat','GA_Opts','GA_Results')
Star Strider
Star Strider 2020 年 9 月 14 日
Thank you!
For some problems .mat files are the only option.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

質問済み:

MB
2020 年 9 月 14 日

コメント済み:

2020 年 9 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by