Saving a structure with the title of a plot as name

Hello,
I am currently trying to get get a function that will both save a newly created figure as a .fig-file with it's title being the file name. That works so far.
However, aside from that, the structure that includes all data used for creating the figure should also be saved under the same name. With this second part, I have troubles.
Is there a way to achieve this? It would surprise me if there isn't a way to do this.
function Savefigfun(LPData)
% Savefig saves the newly created figure as a .fig-file named after the
% title of the figure. The saved figure is still editable afterwards.
CurrentFigureTitle=get(gca,'title');
if isempty(CurrentFigureTitle.String)
warning('Error occured.\nCan''t save a figure without title. Please set title before calling [Savefigfun]','class(n)')
warning off
else
savefig(CurrentFigureTitle.String);
save(CurrentFigureTitle.String,"LPData")
end
% created by Claudius Appel.
% https://de.mathworks.com/matlabcentral/profile/authors/16470428-claudius-simon-appel
end
Thank you.
Have a great day & stay safe
Claudius Appel

6 Comments

Claudius - please clarify what you mean by With this second part, I have troubles. Are you observing an error? If so, what is it? The line of code
save( CurrentFigureTitle.String,"LPData")
seems reasonable assuming that CurrentFigureTitle.String is a string. I do wonder why you use cell2mat in the previous line of code
savefig(cell2mat(CurrentFigureTitle.String));
(?)
I realise my initial question is rather incomplete, so I will correct that.
Regarding the cell2mat:
I just noticed I copied a slightly outdated version into the question. The cell2mat was just me messing with it, trying to see if stuff works. I know that that is a silly approach, but sometimes it works^^.
I edited the code of the original question to make it easier for others to see what I meant.
Elaboration: Aside from saving the figure itself, the function is supposed to save a certain structure (LPData) as a seperate file with the same name as the title-string of the current figure, in order to quickly save the original data of the toolbox LazyPlot I use to plot my data. Inside of LazyPlot, pretty much every output/input is merged into the LPData-struct to make it easier to transport into/out of functions.
Edit: I apparently broke, fixed and rebroke this function code without touching it a single time twice now.
This is a weird experience.
When this line of code
save(CurrentFigureTitle.String,"LPData")
is executed, what happens? If there is an error message, then please copy and paste the full error message to this question. Or maybe nothing happens? Does the file already exist and does the code work when you delete the file? Please clarify.
Okay, I am sorry.
1) The file does not exist, the structure exists in workspace only so far.
2) After execution of the line
save(CurrentFigureTitle.String,"LPData")
I get the error message
Error using save
Must be a string scalar or character vector.
Error in Savefigfun (line 11)
save(CurrentFigureTitle.String,"LPData")
I then tried to apply the same change that fixed the first part when it was suddenly broken.
save(CurrentFigureTitle.String,"LPData")
is replaced by
save(CurrentFigureTitle.String{1},"LPData")
which seems o have fixed it. Now I'll test it a bit and see if it actually works.
I apologize for this mess of a question and my not necessarily on-point answers.
Claudius - it sounds like CurrentFigureTitle.String is a cell array and so when you access the first element with {1}, you get the string that you expect. Alternatively, you could probably do
save(char(CurrentFigureTitle.String),"LPData")
to get the same result.
@Geoff Hayes
Oh, I didn't know that.
I might see which one of the two is more prone to errors.
Every day I learn something new about this program. It is awesome :)
Thank you.

Sign in to comment.

 Accepted Answer

Thank you Geoff Hayes for answering the question and solving the problem.
Link to his comment:
https://www.mathworks.com/matlabcentral/answers/546476-saving-a-structure-with-the-title-of-a-plot-as-name#comment_893957

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!