Having trouble with saving figure of plot

Running a script read raw data from various excel files, plot them individually, then save the plots as .jpeg.
When I run the script the .jpeg removes secondary y axis and accompanying data.
Thoughts?
clc
close all
clear
clear all
%%0 deg
source_dir = 'C:\;
source_files = dir(fullfile(source_dir,'*.xlsx'));
len = length(source_files);
%Importing data from raw data files
for i = 1:1
sheet1 = 1;
Data1 = 'A28:O500';
[num,txt,raw] = xlsread(source_files(i).name,sheet1,Data1);
Data = num2cell(num,1);
%Configure raw data into usable matrices
t = datetime(txt,'InputFormat','MM/dd/yyyy hh:mm:ss a');
V = Data(:,[1,3,5,7,9,11]);
A = Data(:,[2,4,6,8,10,12]);
T = Data(:,[13,14]);
%Pull file name for documentation
[filepath,name] = fileparts(source_files(i).name);
%Re-write name in case underscores exist. MatLab reads as subscript
%callouts
name1 = strrep(name,'_',' ');
%Plot Data
figure()
[ax,h1,h2]=plotyy(t,[V{1} V{2} V{3} V{4} V{5} V{6}],t,[T{1} T{2}]);
set(h2,'linestyle',':')
hold(ax(1),'on');
h3 = plot(ax(1),t,[A{1} A{2} A{3} A{4} A{5} A{6}]);
set(h3,'linestyle','--')
hold off
%Format plot
title(name1);
xlabel('Time (hh:mm:ss)');
ylabel(ax(1),'Voltage / Amperage');
ylabel(ax(2),'Temperature ^{\circ}C');
set(ax(1),'ylim',[0,20]);
lgd = legend('1','2','3','4',.........);
set(lgd,'location','westoutside');
set(gcf,'Position',[680 100 1200 800])
%Print plot, re-sizing nessecary.
print('-djpeg', name1, '-r100');
end

4 Comments

Plotyy is outdated and not recommended. I would try yyaxis and see if the problem persists.
I am using a 2014b version, therefore yyaxis is not and available input.
Does a drawnow before the print help?
Was able to fix by adding the following before print...
set(gcf,'PaperPositionMode','auto')

Sign in to comment.

Answers (1)

clc
close all
clear
clear all
%%0 deg
source_dir='C:\;
source_files=dir(fullfile(source_dir,'*.xlsx'));
len=length(source_files);
%Importing data from raw data files
for i=1:1
sheet1 = 1;
Data1 = 'A28:O500';
[num,txt,raw] = xlsread(source_files(i).name,sheet1,Data1);
Data = num2cell(num,1);
%Configure raw data into usable matrices
t=datetime(txt,'InputFormat','MM/dd/yyyy hh:mm:ss a');
V=Data(:,[1,3,5,7,9,11]);
A=Data(:,[2,4,6,8,10,12]);
T=Data(:,[13,14]);
%Pull file name for documentation
[filepath,name] = fileparts(source_files(i).name);
%Re-write name in case underscores exist. MatLab reads as subscript
%callouts
name1 = strrep(name,'_',' ');
%Plot Data
figure()
[ax,h1,h2]=plotyy(t,[V{1} V{2} V{3} V{4} V{5} V{6}],t,[T{1} T{2}]);
set(h2,'linestyle',':')
hold(ax(1),'on');
h3=plot(ax(1),t,[A{1} A{2} A{3} A{4} A{5} A{6}]);
set(h3,'linestyle','--')
hold off
%Format plot
title(name1);
xlabel('Time (hh:mm:ss)');
ylabel(ax(1),'Voltage / Amperage');
ylabel(ax(2),'Temperature ^{\circ}C');
set(ax(1),'ylim',[0,20]);
lgd=legend('1','2','3','4',.........);
set(lgd,'location','westoutside');
set(gcf,'Position',[680 100 1200 800])
%Display the figure as per requirements-
%Following code save the exact display figure in destination folder
destination='D:\path\folder_name\im'; %im here save file initial name im1, im2,im3...
saveas(gcf,[destination,num2str(i),'.jpg']);
end

Categories

Find more on Printing and Saving in Help Center and File Exchange

Asked:

on 17 Sep 2018

Edited:

on 18 Sep 2018

Community Treasure Hunt

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

Start Hunting!