How can I save (and later recall) a graph as a variable?

64 views (last 30 days)
I'm relatively new to MatLab so my issue may be a very basic thing I'm missing. I have a main image in which I want to iteratively select separate blobs (say I want to analyze 5 of them) delimitate a ROI and analyze the mean pixel value VS distance from the centroid of the blob, or VS the angle. So far I can display each graph in succession, but when the code is done running, it will only show the last plot (which is normal). I want to save each plot as a variable so that after the code is done running, I want to display, say, the second blob's graph to check the data.
So far I am able to save the blobs as separate images in a variable such as data(it).blob from which I would call imshow(data(2).blob) to show the image of the second blob. How can I do a similar thing, but for graphs? I tried the following, but it didn't seem to work:
plotData(it) = plot(something-or-the-other);
...then calling imshow(plotData(2)) but that doesn't seem to work. Any ideas? Thanks in advance!

Answers (1)

Rahul
Rahul on 5 Sep 2024
I understand that you are trying to save graphs as variables and recall them later when required in your code. However, unlike images or data, plots aren't typically stored as variables directly. Instead, you can save the figures themselves.
You can follow the below mentioned code to achieve the same:
numBlobs = 5; % Assumption, can be changed
% Preallocate a structure to store figure handles
plotData(numBlobs) = struct('figureHandle', []);
for it = 1:numBlobs
fig = figure;
% Assuming 'blobData' as the data to be plotted- can be changed accordingly
plot(blobData(it));
% Storing the figure handle in 'plotData'
plotData(it).figureHandle = fig;
end
Note: In the mentioned method, if the figures are closed, then the 'figure handles' get deleted so 'plotData' will contain 'deleted figure handles' which is not desirable.
So another workaround for this case can be using 'saveas' function to save the 'figure' as a '.fig' file. Using this the figures can be displayed whenever needed using the 'openfig' function:
saveas(fig, sprintf('blobFigure_%d.fig', it));
% This line can be added as the last line of the above loop to save all the
% figures iteratively.
openfig('blobFigure_1.fig');
% This line can be used separately while trying to access the saved 'figure'
You can refer to the following documentations to know more abouts these functions:
Hope this helps! Thanks.

Categories

Find more on Images 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!