Highlight graphic object stored in .mat file with new Matlab graphics engine
UPDATE # 1:
So, it seems like the following approach is a little simpler than using the handle2struct and struct2handle functions in Matlab. Basically, I just save the curly handles in an array and then save the array using the savefig function.
[X,Y] = meshgrid(-8:0.5:8);
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;
% 1: Save 4 figures to an array called 'H':
for i = 1 : 4
H(i) = figure('Visible','Off');
surf(X,Y,Z);
end
savefig(H,'fig_container.fig','compact');
close(H)
clearvars
% 2: Open 4 figures:
figs = openfig('fig_container.fig','visible');
for i = 1 : 4
H(i) = figs(i);
end
However, I seem to be losing some functionality by not using the h2s / s2h functions - in particular, I am forced to use the openfig function to load the saved fig file ... it's bad news if I've saved hundreds or even thousands (millions?) Of digits in one array in the fig file, because using the openfig function I'm loading the whole thing . This seemed to be the main benefit of using the h2s / s2h functions ... I could save the structures to a mat file and then use the matfile function to load each structure separately (if I don't understand the matfile function correctly). The following code shows how I used the matfile functionality:
[X,Y] = meshgrid(-8:0.5:8);
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;
fig_container = cell(4,1);
for i = 1 : 4
fig = surf(X,Y,Z);
figure_in = struct2cell(handle2struct(gcf));
fig_container{i,1} = figure_in;
clearvars figure_in
end
clearvars -except fig_container
close all
save fig_container.mat fig_container -v7.3;
figure_final = figure;
name_cell = {'type','handle','properties','children','special'};
for i = 1 : size(fig_container,1)
matObj = matfile('fig_container.mat');
single_cell = matObj.fig_container(i,1);
lime = single_cell{1,1};
loadedData = cell2struct(lime,name_cell,1);
figure_sub = subplot(2,2,i);
figure_out = struct2handle(loadedData,0,'convert');
figure_out_children = get(get(figure_out,'children'),'children');
set(figure_out_children,'parent',figure_sub);
set(figure_out_children,'FaceColor','interp','EdgeColor','none');
r1 = randi(91)-1;
r2 = randi(91)-1;
set(gca,'View',[r1 r2]);
close(figure_out);
clearvars figure_out figure_sub
end
clearvars -except fig_container figure_final
% close all
I could keep using this method, but there must be a way to load parts of the fig file variable without loading the whole variable ... I prefer the simplicity of the savefig / openfig approach a lot, but I can't find a way to capture only one saved figure at a time.
Any ideas?
Justin
For historical purposes, here's my first post:
I am new to using figs as objects in Matlab 2015a (feature introduced in Matlab 2014b). I would like to save multiple files fig
in a file mat
and then retrieve them later for plotting. Since 2014b and later are relatively new, I cannot find what I was looking for in a regular web search. The following code 4 stored numbers called figure_in_1
, figure_in_2
, figure_in_3
and figure_in_4
, each of which has a value 1x1 Surface
and a class matlab.graphics.chart.primitive.Surface
:
[X,Y] = meshgrid(-8:0.5:8);
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;
for i = 1 : 4
cmd = horzcat('figure_in_',num2str(i),' = surf(X,Y,Z)'); eval(cmd);
if exist('fig_container.mat','file')
save('fig_container.mat',horzcat('figure_in_',num2str(i)),'-append');
else
save('fig_container.mat',horzcat('figure_in_',num2str(i)));
end
end
clearvars -except fig_container
close all
What I would like to do is grab these graphics and plot them. So the question is, how do I grab and build an already saved Matlab graphic object?
Justin :)
source to share
There are quite a few things that are considered bad practice in your code, especially usingeval
, in your case, to create variable names from strings. The best option is to use structs with field names of type string.
This is how I would do it:
%// data
[X,Y] = meshgrid(-8:0.5:8);
R = sqrt(X.^2+Y.^2)+eps;
Z = sin(R)./R;
%// struct with surfaces, this is where all surface graphics will be
surfaces= struct;
%// loop creating all figures and storing it
%// create new figure every iteration, plot the surface, save its handle with a dynamic
%// name into the surfaces struct
for ii = 1 : 4
figure
surfaces.(['figure_in_',num2str(ii)]) = surf(X,Y,Z);
%// save to matfile
save('fig_container.mat','surfaces','-append')
end
clear
close all
load('fig_container.mat')
%// loop for displaying
for ii = 1 : 4
%// create new figure and axes
figure; axes;
%// get surface froms truct (this line can be abolished and used directly below)
sur = surfaces.(['figure_in_',num2str(ii)]);
%// assign parent axes object for displaying
set(sur,'parent',gca)
end
If you really don't want to store surfaces in a structure in your layout file, use a construct anyway, but use this command to store:
save('fig_container.mat','-struct', 'surfaces', fieldname,'-append')
Now, your shape container will not contain a struct surfaces
, it will contain all the descriptors as separate variables:
>> whos
Name Size Bytes Class
figure_in_1 1x1 112 matlab.graphics.chart.primitive.Surface
figure_in_2 1x1 112 matlab.graphics.chart.primitive.Surface
figure_in_3 1x1 112 matlab.graphics.chart.primitive.Surface
figure_in_4 1x1 112 matlab.graphics.chart.primitive.Surface
And you can build one surface, simply:
figure; axes;
set(figure_in_1,'parent',gca)
or even easier
set(figure_in_1,'parent',axes) %// but no new figure window will be opened,
%// if desired call figure before
or in subplot
figure;
subplot(121)
set(figure_in_1,'parent',gca)
subplot(122)
set(figure_in_2,'parent',gca)
source to share