How can I save current figure in a structure as .fig?

18 ビュー (過去 30 日間)
Levente Gellért
Levente Gellért 2024 年 1 月 22 日
コメント済み: Levente Gellért 2024 年 2 月 9 日
Dear Community,
I am saving my variables in a structure like "s = struct(field,value....."). I could not find the way to save the current figure appearing at the end of my code to save along with the other variables in the same structure as .fig.
Suggestions are appreciateed.
lg

採用された回答

Hassaan
Hassaan 2024 年 1 月 22 日
Directly saving a figure handle inside a structure is not straightforward because the figure handle is a reference to a figure object, not the figure data itself.
Save the Figure to a File: First, save your figure to a .fig file using the savefig function. This step creates a file that contains all the data needed to recreate the figure.
Store the File Path in the Structure: Instead of storing the figure handle or the figure object itself in your structure, you store the path to the .fig file. This way, your structure will contain a reference to the figure which can be loaded later.
% Assume you have a figure
figure;
plot(rand(5));
title('Sample Figure');
% Save the figure to a file
figFileName = 'myFigure.fig';
savefig(figFileName);
% Now, create your structure and include the file name
s = struct('field1', value1, 'field2', value2, ..., 'figureFile', figFileName);
% You can later load the figure using openfig
% loadedFigure = openfig(s.figureFile);
  • Replace 'field1', value1, 'field2', value2, etc., with your actual field names and values.
  • The figure is saved as 'myFigure.fig' in the current directory. You can specify a different path if necessary.
  • The path to the figure file is stored in the structure s under the field 'figureFile'.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 件のコメント
Levente Gellért
Levente Gellért 2024 年 2 月 9 日
Dear Hassaan , thanks for your suggestion! best lg

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by