Plot files with legend names indicated from a value from each file

Hello there,
I have 4 files, each of them containing 3 parameters in a matlab table, namely: z, station, and temp. I made a loop to plot the z-temp. My intention is I want to get the legend for each station plot with the number (name) of the station, where the name of each station is indicated in column station in each file of the station. See the files attached.
Here is my code so far which is unfortunatelly (offcourse) it gives a 'common' legend. It must be 11, 13, 15, 18 indeed.
clear all;
clc;
A=dir('*.mat');
numprof = length(A);
couleur = jet(numprof);
for nn=1:length(A)
filename = A(nn).name;
B = load(filename);
z = B.T.z(:,1);
T = B.T.temp(:,1);
sta = B.T.station(:,1);
plot(T,z,'LineWidth',1,'color',couleur(nn,:));
hold on;
end
legend('show'); % the legend I want is the name of the station, indicated from coulumn sta of each statio file, not the number of the profile
set(gca,'Ydir','reverse')

Réponses (2)

All the files have the same format, so this is straightforward —
mats = dir('*.mat');
numprof = numel(mats);
couleur = jet(numprof);
for k = 1:numel(mats)
matname{k,:} = mats(k).name;
LD = load(matname{k});
T{k} = LD.T;
end
matname{:}
ans = 'statio_a.mat'
ans = 'statio_b.mat'
ans = 'statio_c.mat'
ans = 'statio_d.mat'
figure
hold on
for k = 1:numel(T)
D = T{k};
station1 = extractBefore(matname{k},'.'); % Station Names With Subscripts
station2 = strrep(extractBefore(matname{k},'.'),'_','\_'); % Station NNames As In Original File Names
plot(D.z, D.temp, DisplayName=station2, LineWidth=1, Color=couleur(k,:))
end
hold off
grid
xlabel('z')
ylabel('temp')
legend('Location','best')
There are two options for the legend entries, one with the different fille names as subscripts, and one wiith the names as they exist. This example uses the second option.
EDIT — Corrected typographical errors.
.

3 commentaires

Anyway, my last questions for this thread:
(1) Is it possible to add a title for the legend in matlab? For example "Station Number" as the title of the legend.
Yes. However, I do not now remember when that was added. It is possible in R2024b, and several earlier releases.
(2) From my example files above, is it possible to get the maximum (the highest) and minimum (the lowest) value from all files? For example, I want to get the highest and lowest temp (considering all values of temp from all stations).
Yes. Here, the easiest way is to use the bounds function to extract both into separate vectors, and then take the maximum and minimum of the respective vectors. (I also did that to get the maximum value for ‘z’ in order to position the minimum valuee text object accurately and adaptively.)
Try this —
mats = dir('*.mat');
numprof = numel(mats);
couleur = jet(numprof);
for k = 1:numel(mats)
matname{k,:} = mats(k).name;
LD = load(matname{k});
T{k} = LD.T;
end
matname{:}
ans = 'statio_a.mat'
ans = 'statio_b.mat'
ans = 'statio_c.mat'
ans = 'statio_d.mat'
figure
hold on
for k = 1:numel(T)
D = T{k};
station1 = extractBefore(matname{k},'.'); % Station Names With Subscripts
station2 = strrep(extractBefore(matname{k},'.'),'_','\_'); % Station NNames As In Original File Names
plot(D.z, D.temp, DisplayName=station2, LineWidth=1, Color=couleur(k,:))
[temp_min(k),temp_max(k)] = bounds(D.temp);
[z_min(k),z_max(k)] = bounds(D.z);
end
temp_max = max(temp_max);
temp_min = min(temp_min);
z_max = max(z_max);
text(75, temp_max, sprintf('\\leftarrow T_{max} = %.2f°C',temp_max), 'Horiz','left', 'Vert','middle')
text(z_max, temp_min, sprintf('T_{min} = %.2f°C\n\\downarrow',temp_min), 'Horiz','left', 'Vert','bottom')
hold off
grid
xlabel('z')
ylabel('temp')
hlgd = legend('Location','best');
title(hlgd, 'Station Number')
.
Thank you @Star Strider! That's really helping!

Connectez-vous pour commenter.

plot(T,z,'LineWidth',1,'color',couleur(nn,:), 'DisplayName', string(sta(1)));

1 commentaire

Adi Purwandana
Adi Purwandana le 8 Nov 2024
Modifié(e) : Adi Purwandana le 8 Nov 2024
Thank you @Walter Roberson for the solution and also @Star Strider for such fruitful additional solutions I may use for future projects. Great!
Anyway, my last questions for this thread:
(1) Is it possible to add a title for the legend in matlab? For example "Station Number" as the title of the legend.
(2) From my example files above, is it possible to get the maximum (the highest) and minimum (the lowest) value from all files? For example, I want to get the highest and lowest temp (considering all values of temp from all stations).

Connectez-vous pour commenter.

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by