Save a plot from a function in a structure and save the structure

9 次查看(过去 30 天)
Hi everyone.
I created a function to perform pca calculations and to plot some specific plots, like loadings and scores.
to run this in my scrip i type:
[PCA_Model] = functionPCA(X)
This creates me a structure, where I have variables saved there that I can access using PCA_Model.eigenvalues, PCA_Model.Scores, etc.
Question -> How can I save this structure to access later on, instead of running the script again? Also, the plots I made is there a way to save in the structure like the variables? My objective in a fold of my chosen, I open PCA_Model, and I can see the plots I created plus the variables I saved in the structure

采纳的回答

Fangjun Jiang
Fangjun Jiang 2018-10-18
You can save all your workspace data or selected data into an .mat file. Then you can load or simply double click the .mat file to load the data back to the workspace. You can access the data whatever you want. See
doc save
doc load
For plots, you can also save them in separated files. On your plot, click menu, File, Save or Save As. Then you can re-open the plots by double click or drag-and-drop.
If it does not take too much time to run functionPCA(), I would still recommend calling and running your function every time, instead of saving to various files. Keep your original (source) data. The derived data can always be re-produced.
  4 个评论
Tiago Dias
Tiago Dias 2018-10-19
But since I have to run my functionPCa several times, because of different datasets.
If I use saveas inside the function, the name that I use to save them will be the same each time I run the functionPCA in the script.
[PCA1] = functionPCA(X) -> will result in 1 plot for example.
[PCA2] = functionPCA(XX) -> will result in 1 plot for example.
function Model = functionPCA(X)
example = figure;
plot(X,Y)
saveas example
PCA1 will save 1 plot, PCA2 will save a different plot but with the same name. My problem, is how to save the plots, so that I have a dynamic name to save them, so I can tell all plots from PCA1 are saved in a different file name from PCA2
(hope not to confuse)
Fangjun Jiang
Fangjun Jiang 2018-10-19
You could provide extra input arguments to your function, e.g. functionPCA(X,'SavedDataFileName','SavedFigureName') to get what you want. Or utilize the function inputname() to recognize the name of X or XX as your input and name files diffrently. But anyway, it will not be a pretty architecture or programming flow if you go down that way. I would split your program into 3 parts.
  1. functinpPCA(),process the input and create the output
  2. save the data, could be just one command, no function needed.
  3. load the data, create the plot

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by