MATLAB: Save a created .gif into defined file location

directoryfilegifimageslocationsavesaveas

Hi,
So I have some code that runs though some saved data, processes it, and creates a gif, as shown below;
OUTPUT_FILE
for count = 1:50;
%various code for loading and processing data
......
......
......
......
%create figures for each file
data_figure = figure('Visible','off');
axes1 = axes('Parent', data_figure, 'ZGrid', 'on', 'YGrid', 'on');
view(axes1,[0.5 90]);
xlim(axes1,[-0.01 0.25]);
hold(axes1, 'all');
surf(data_A,data_B,data_C, 'Edgecolor', 'none');
colorbar;
title('......');
xlabel('......');
ylabel('......');
set(gca, 'XGrid', 'off');
ylabel(colorbar, '.....');
%save images in OUTPUT_FILE
saveas(gcf, fullfile(OUTPUT_FILE, [num2str(count),'.png']));
%create gif
drawnow;
frame = getframe(data_figure);
im = frame2im(frame);
[AA,map] = rgb2ind(im,256);
if count == 1,
imwrite(AA,map,gif_name,'gif','LoopCount',Inf,'DelayTime',0.5);
else
imwrite(AA,map,gif_name,'gif','WriteMode','append','DelayTime',0.5);
end
end
How do I set it to save the gif into the OUTPUT_FILE location, as I have done with the individual images?
Thanks

Best Answer

Assuming that OUTPUT_FILE contains the full path of the file, and gif_name is just a filename,
path = fileparts(OUTPUT_FILE);
gif_name = fullfile(path, gif_name);
If gif_name also contains a path that you want to discard:
[~, gif_filename, gif_extension] = fileparts(gif_name);
gif_name = fullfile(path, [gif_filename '.' gif_extension]);