Opening a workspace plot stored in a variable. How?

Hello, my friends
Given the code bellow:
>> x = -pi:pi/20:pi;
>> y = sin(x);
>> f = figure;
>> p = plot(x,y);
>> txt1 = text(0.2,0,'sin(x)');
>> p.Color='r'; %close the plot after this change.
>> p.Color='g';
Invalid or deleted object.
I wanted to change the plot line from red to green but, first of all, I noticed I can't do it without the plot being open. Hence the error at the end. Is this right?
Next, this code (hence the variables too) is not saved in any file so how do I open the plot again? It's stored as a variable p not saved any where.
Thanks

 Risposta accettata

See print() and saveas() and hgsave()
Closing a plot deletes it. If you just want to make it invisible without deleting it, you set its Visible property to 'off'

8 Commenti

Maybe my question was not clear enough. I want to see the plot again after closing it. How do I open it again without having to use plot(x,y);?
Closing a plot is defined as deleting it. It is gone after you close it.
Your options include:
  • not closing it! -- make it invisible if you don't want to see it for a time
  • saving it as an image using print() or saveas(), and later bring back the image using imread() and image() or imshow()
  • do a screen capture and saveas() or imwrite(), and later bring back the image using imread() and image() or imshow()
  • use hgsave() or savefig() to save the graphics entities, and later bring them back as graphics entities using hgrestore() or openfig()
  • if you have R2014b or later (which you appear to) then you can save() the graphics objects, and later bring them back using load()
One thing that I suspect you missed is that plot() does not create an entire plot. plot() is a routine that creates line() objects [older versions] or lineseries objects (the latter versions leading up to R2014a) or chart line objects . Just the line parts. When there is no existing axes, an axes is automatically generated in the figure. When there is no existing active figure, a figure is automatically generated. But the return value from the plot() call is only the line objects (you can get to the axes or figure by examining the properties of the line objects.)
For versions up to and including R2014a, the return value from plot is one or more "graphics handle" which are double precision values that the graphics system looks up in an internal table to find the actual graphics data structure. In versions starting from R2014b, the return value from plot is one or more Object Oriented objects that are unprintable objects that refer more directly to the graphics data structure.
If you want to display a graphics object and there is a risk that the enclosing plot will be closed and thereby typically deleting the graphics object, then make a copy of the object. I am not entirely clear at the moment if you can simply
copy_of_p = p;
or if more will need to be done to clone the handle object. I do not have that version to test with.
Many thanks, Walter, for the detailed explanation. I've been doing some experiments with your info, and for now, one question is: when I define p = plot(x,y); if I understood correctly, this variable is used to define/change the line properties (p.propertie='value';).
Probably because it's not allowed to write plot(y).Color='r'; for ex.
Is this right?
MATLAB does not happen to allow you to apply methods to the results of function calls, so you will need to store the result into a variable like you show.
Another thing I've noticed is that if I close the plot (line + fig) everything gets deleted. In this case I can't do any change in the properties unless I plot it again. What is the usual (or most used) procedure to change properties (color, for ex) after closing the plot? I guess first I need to plot it again...
When you close something you are closing the figure. Everything contained in the figure is deleted (unless there is some other reference to it.)
rbarata
rbarata il 25 Nov 2015
Modificato: rbarata il 25 Nov 2015
So, if a line is created by the function plot() it only exists in the figure object, or is supported by this object. If the support does not exist anymore, everything inside it gets deleted. But the same line, defined by a function (in this case and x = -pi:pi/20:pi; >> y = sin(x);) exist but cannot be seen.
The same can be said about axes, they are part of the figure and serve as a support for the line plot. This is the base concept of Object Groups, I think.
So, the plot() function is meant to be used only when one wants to see the line and nothing more.
Am I right?
y = sin(x) creates a numeric variable, not a plot. The numeric variable will continue to exist as long as there is a reference to it; typically the variable will disappear as when the function returns.
plot() is for creating line graphics. The graphics can be made invisible, but closing the figure deletes the graphics unless they are saved somewhere.
When you close a figure by using your operating system window manager to click on the 'X', you are running close() which you should read the documentation for, including following through to http://www.mathworks.com/help/matlab/ref/figure-properties.html#prop_CloseRequestFcn
"The closereq function unconditionally deletes the current figure, destroying the window"

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Printing and Saving in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by