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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by