Subplot title lost depending on data

16 Ansichten (letzte 30 Tage)
Mark Rulman
Mark Rulman am 5 Jan. 2018
Bearbeitet: Adam am 5 Jan. 2018
Hello,
I have recently started using Matlab and am currently trying to write code to automate the plotting of some data in a 2x2 subplot. I think I have managed to get the code to do that, but I am having problems with the titles of the subplots. Depending on if whole data sets are present or missing, the title of the graphs are mislabelled. When I have all of the data sets present (there are 4 batches of data subdivided in to 8 sets), the titles are correct. If there is a batch missing the titles are mislabelled. I have attached the M file and some example data sets so the problem can be seen first hand. The code for plotting is as follows:
if Batch1Al>0
for k=1:length(Batch1A)
subplot(2,2,1);
legendinfo1A{k}=['B1A - Set ', num2str(Batch1Aleg{k})];
scatter(Time,Batch1.Data(indSet1A{(Batch1A(k))}),'x');
hold on
end
end
if Batch1Bl>0
for k=1:length(Batch1B)
subplot(2,2,1);
legendinfo1B{k}=['B1B - Set ', num2str(Batch1Bleg{k})];
scatter(Time,Batch1.Data(indSet1B{(Batch1B(k))}),'*');
hold on
end
end
% %%%%%%%%%%Concatenate legend entries for Batch 1 graph and display them
legendinfo1=[legendinfo1A legendinfo1B];
% Removes any cells with a value of 0 from the concatenated array
legendinfo1=legendinfo1(cellfun(@(x) ~isequal(x,0),legendinfo1));
legend(legendinfo1);
title('Batch 1')
ylabel('Data','FontWeight','bold');
xlabel('Time','FontWeight','bold');
This block of code is repeated four times, once for each batch of data (with the appropriate changes to subplot calls and plot titles). If anyone can see a problem with the code or knows why the title is mislabelled when a batch of data is missing, please let me know.
Thanks.
  1 Kommentar
Adam
Adam am 5 Jan. 2018
Bearbeitet: Adam am 5 Jan. 2018
I don't have time to look at the whole code to work out exactly what is happening, but at a guess the reliance on the current axes being the correct one may well be a source of problems, it often is.
doc subplot
includes a return argument which is the handle of the axes you just created.
doc hold
doc title
doc xlabel
doc ylabel
etc (basically any plotting or annotation function that works on an axes)
all include options to define the axes handle to which they will be applied. Get used to always using explicit handles for these things rather than just relying on the current axes which is the default and is very brittle.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by