Opening a workspace plot stored in a variable. How?

18 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 23 Nov 2015
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 Comments
rbarata
rbarata on 25 Nov 2015
Edited: rbarata on 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?
Walter Roberson
Walter Roberson on 26 Nov 2015
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"

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!