Results for
Starting in MATLAB R2022a, use the append option in exportgraphics to create GIF files from animated axes, figures, or other visualizations.
This basic template contains just two steps:
% 1. Create the initial image file gifFile = 'myAnimation.gif'; exportgraphics(obj, gifFile);
% 2. Within a loop, append the gif image for i = 1:20
% % % % % % % % Update the figure/axes % % % % % % % %
exportgraphics(obj, gifFile, Append=true); end
Note, exportgraphics will not capture UI components such as buttons and knobs and requires constant axis limits.
To create animations of images or more elaborate graphics, learn how to use imwrite to create animated GIFs .
Share your MATLAB animated GIFs in the comments below!
See Also
This Community Highlight is attached as a live script
You've spent hours designing the perfect figure and now it's time to add it to a presentation or publication but the font sizes in the figure are too small to see for the people in the back of the room or too large for the figure space in the publication. You've got titles, subtitles, axis labels, legends, text objects, and other labels but their handles are inaccessible or scattered between several blocks of code. Making your figure readable no longer requires digging through your code and setting each text object's font size manually.
Starting in MATLAB R2022a, you have full control over a figure's font sizes and font units using the new fontsize function (see release notes ).
Use fontsize() to
- Set FontSize and FontUnits properties for all text within specified graphics objects
- Incrementally increase or decrease font sizes
- Specify a scaling factor to maintain relative font sizes
- Reset font sizes and font units to their default values . Note that the default font size and units may not be the same as the font sizes/units set directly with your code.
When specifying an object handle or an array of object handles, fontsize affects the font sizes and font units of text within all nested objects.
While you're at it, also check out the new fontname function that allows you to change the font name of objects in a figure!
Give the new fontsize function a test drive using the following demo figure in MATLAB R2022a or later and try the following commands:
% Increase all font sizes within the figure by a factor of 1.5 fontsize(fig, scale=1.5)
% Set all font sizes in the uipanel to 16 fontsize(uip, 16, "pixels")
% Incrementally increase the font sizes of the left two axes (x1.1) % and incrementally decrease the font size of the legend (x0.9) fontsize([ax1, ax2], "increase") fontsize(leg, "decrease")
% Reset the font sizes within the entire figure to default values fontsize(fig, "default")
% Create fake behavioral data rng('default') fy = @(a,x)a*exp(-(((x-8).^2)/(2*3.^2))); x = 1 : 0.5 : 20; y = fy(32,x); ynoise = y+8*rand(size(y))-4; selectedTrial = 13;
% Plot behavioral data fig = figure('Units','normalized','Position',[0.1, 0.1, 0.4, 0.5]); movegui(fig, 'center') tcl = tiledlayout(fig,2,2); ax1 = nexttile(tcl); hold(ax1,'on') h1 = plot(ax1, x, ynoise, 'bo', 'DisplayName', 'Response'); h2 = plot(ax1, x, y, 'r-', 'DisplayName', 'Expected'); grid(ax1, 'on') title(ax1, 'Behavioral Results') subtitle(ax1, sprintf('Trial %d', selectedTrial)) xlabel(ax1, 'Time (seconds)','Interpreter','Latex') ylabel(ax1, 'Responds ($\frac{deg}{sec}$)','Interpreter','Latex') leg = legend([h1,h2]);
% Plot behavioral error ax2 = nexttile(tcl,3); behavioralError = ynoise-y; stem(ax2, x, behavioralError) yline(ax2, mean(behavioralError), 'r--', 'Mean', ... 'LabelVerticalAlignment','bottom') grid(ax2, 'on') title(ax2, 'Behavioral Error') subtitle(ax2, ax1.Subtitle.String) xlabel(ax2, ax1.XLabel.String,'Interpreter','Latex') ylabel(ax2, 'Response - Expected ($\frac{deg}{sec}$)','Interpreter','Latex')
% Simulate spike train data ntrials = 25; nSamplesPerSecond = 3; nSeconds = max(x) - min(x); nSamples = ceil(nSeconds*nSamplesPerSecond); xTime = linspace(min(x),max(x), nSamples); spiketrain = round(fy(1, xTime)+(rand(ntrials,nSamples)-.5)); [trial, sample] = find(spiketrain); time = xTime(sample);
% Spike raster plot axTemp = nexttile(tcl, 2, [2,1]); uip = uipanel(fig, 'Units', axTemp.Units, ... 'Position', axTemp.Position, ... 'Title', 'Neural activity', ... 'BackgroundColor', 'W'); delete(axTemp) tcl2 = tiledlayout(uip, 3, 1); pax1 = nexttile(tcl2); plot(pax1, time, trial, 'b.', 'MarkerSize', 4) yline(pax1, selectedTrial-0.5, 'r-', ... ['\leftarrow Trial ',num2str(selectedTrial)], ... 'LabelHorizontalAlignment','right', ... 'FontSize', 8); linkaxes([ax1, ax2, pax1], 'x') pax1.YLimitMethod = 'tight'; title(pax1, 'Spike train') xlabel(pax1, ax1.XLabel.String) ylabel(pax1, 'Trial #')
% Show MRI pax2 = nexttile(tcl2,2,[2,1]); [I, cmap] = imread('mri.tif'); imshow(I,cmap,'Parent',pax2) hold(pax2, 'on') th = 0:0.1:2*pi; plot(pax2, 7*sin(th)+84, 5*cos(th)+90, 'r-','LineWidth',2) text(pax2, pax2.XLim(2), pax2.YLim(1), 'ML22a',... 'FontWeight', 'bold', ... 'Color','r', ... 'VerticalAlignment', 'top', ... 'HorizontalAlignment', 'right', ... 'BackgroundColor',[1 0.95 0.95]) title(pax2, 'Area of activation')
% Overall figure title title(tcl, 'Single trial responses')
This Community Highlight is attached as a live script.
Several major updates have been introduced to Answers’ reputation system! The updates include a new User Levels system, a new Editor indicator, and updated badges series.
1. User Levels
User Levels have become a best practice for many community sites to adopt. They help build trust in the community and provide recognition to contributors. There are 10 levels in the system and the labels will display next to users’ names throughout MATLAB Answers and on your community profile. We hope to see more users climb the ladder and level up!
2. Editor Indicators
Becoming an Editor (upon earning 3,000 points) is a huge milestone in Answers. Therefore, we introduced the Editor indicator to show our appreciation. From the screenshot below, you will notice a user can have both a User Level and an Editor indicator.
3. Updated Badge Series
Based on our analysis of existing badges, we decided to introduce 2 new badges into existing series and retire an entire badge series.
- The Knowledgeable badge series and the Thankful badge series now have 5 levels.
- The Revival badge series has been archived and is no longer being awarded. If you earned one of these badges, it would still show up in your community profile.
You will find more information on Answers help page . If you have any questions, comments or feedback, free feel to leave a comment below.
Cody is a useful tool to practice MATLAB skills not only by solving the problems but also learn from each other’s solutions. Sometimes you see subpar solutions that are cheats and hacks. With the flagging feature we released recently, you can help us identify solutions that administrators, including Community Advisory Board members, can review and delete.
How to flag?
Flag Options - Only available on solutions
Every day, thousands of people ask questions on MATLAB Answers and many of these are about their code. Questions such as “How can I make this faster?”, “Why do I get this error message?” or “Why don’t I get the answer I expect?”. There’s often one crucial thing missing though – the code in question!
Most of the people who answer questions on MATLAB Answers are volunteers from the community. They are answering your questions for fun, to learn more about MATLAB or just because they like to be helpful. This is even true for people such as me who are MathWorks members of staff. It’s not part of my role to patrol the community, looking where I can help out. I do it because I like to do it.
Make it easier to help me help you.
Imagine you’re a volunteer, looking for something interesting to answer. What kind of questions are you more likely to dig into and help an anonymous stranger figure out?
In my case, I almost always focus on problems that I can easily reproduce. I rarely know the answer to any question off the top of my head and so what I like to do is start off with the problem you are facing and use the various tools available to me such as the profiler or debugger to figure it out. This is the fun of it all for me – I almost always learn something by doing this and you get helped out as a side effect!
The easier I can reproduce your issue, the more likely I am to get started. If I can’t reproduce anything and the answer isn’t immediately obvious to me I’ll just move onto the next question. One example that demonstrates this perfectly is a case where someone’s MATLAB code was running too slowly. All of the code was available so I could run it on my machine, profile it and provide a speed-up of almost 150x.
It's not always feasible or desirable to post all of your code in which case you need to come up with a minimal, reproducible example. What’s the smallest amount of code and data you can post that I can run on my machine and see what you see? This may be more work for you but it will greatly increase your chances of receiving an answer to your question.