Minimalist
Minimalist

Reputation: 975

MATLAB: Selected title, xlabel, ylabel for Plots in a For Loop

In Matlab, I’m outputting a series of plots by a for loop. The data iterating through the for loop to be plotted is constructed in a multidimensional matrix. However I need the title, xlabel, and ylabel in the for loop to change its selected string for each iteration through the for loop.

Here is the code:

lat = [40 42 43 45 56]'
lon = [120 125 130 120 126]'
alt = [50 55 60 65 70]'
time = [1 2 3 4 5]'
position = cat(3, lat, lon, alt);

for k = 1:3
figure
plot(time, position(:,k),'LineWidth', 2, 'Color', 'b')
xlabel('Latitude Time');
ylabel('Latitude Mag');
title('Time v. Latitude');
end 

How do I get the plots to output the labels in the for loop as:

First Iteration:

xlabel = Latitude Time ylabel = Latitude Mag title = Time v. Latitude

Second Iteration:

xlabel = Longitude Time ylabel = Longitude Mag title = Time v. Longitude

Third Iteration:

xlabel = Altitude Time ylabel = Altitude Mag title = Time v. Altitude

Upvotes: 0

Views: 270

Answers (1)

am304
am304

Reputation: 13876

As suggested in the comments, use a cell array for your labels and index into it:

my_xlabels = {'Latitude Time';'Longitude Time';'Altitude Time'};
my_ylabels = {'Latitude Mag';'Longitude Mag';'Altitude Mag'};
my_titles = {'Time v. Latitude';'Time v. Longitude';'Time v. Altitude'};

for k = 1:3
   figure
   plot(time, position(:,k),'LineWidth', 2, 'Color', 'b')
   xlabel(my_xlabels{k});
   ylabel(my_ylabels{k});
   title(my_titles{k});
end 

Upvotes: 1

Related Questions