Titles of Subplot which re depend on Multiselect File Title

2 views (last 30 days)
Tyann Hardyn
Tyann Hardyn on 12 Aug 2021
Answered: Bjorn Gustavsson on 12 Aug 2021
Hi everyone, i want to ask about how to create subplot title which re acquired from multiselect files data we choose. Here is the general script i made but its never works, and make me confused....
[namafile,direktori]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load Data Magnet LEMI RAW Non IAGA (Menitan / Detikan)','Multiselect','on');
full = fullfile(direktori,namafile);
nfiles = size(full,2);
f = cellstr(fullfile(direktori,namafile));
file = length(f);
if file > 1
for k = 1:nfiles
[~, namafile{k}] = fileparts(full{k});
nama = namafile{k};
lastIndex2 = length(nama);
% Capitalize first character and lower case remaining characters.
nb2 = [upper(nama(1)), lower(nama(2:lastIndex2))];
tgl = nb2(end-7:end); %This variable is suppose to be my subplot title
B = readmatrix(full{k}, opts);
tahun = cellfun(@str2num,(B(1:end,1))); %: Getting data from column 1
bulan = cellfun(@str2num,(B(1:end,2))); %: Getting data from column 2
tanggal = cellfun(@str2num,(B(1:end,3))); %: Getting data from column 3
jam = cellfun(@str2num,(B(1:end,4))); %: Getting data from column 4
menit = cellfun(@str2num,(B(1:end,5))); %: Getting data from column 5
detik = cellfun(@str2num,(B(1:end,6))); %: Getting data from column 6
x = cellfun(@str2num,(B(1:end,7))); %: Getting data from column 7
y = cellfun(@str2num,(B(1:end,8))); %: Getting data from column 8
z = cellfun(@str2num,(B(1:end,9))); %: Getting data from column 9
h = cellfun(@str2num,(B(1:end,10))); %: Getting data from column 10
D = datetime(tahun,bulan,tanggal,0,0,0,'TimeZone','Asia/Jakarta');
time = hours(jam) + minutes(menit) + seconds(detik);
time.Format = 'hh:mm:ss';
wak = D + time; % Here is the X-Axis for plotting in the subplot
%which re acquired from each multiselect file data
wak_combine(:,k) = wak(:);
wak = wak(:);
DOHk = sqrt((x.^2)+(y.^2)); % Here is the Y-Axis for plotting in the subplot
%which re acquired from each multiselect file data
DOHk_combine(:,k) = DOHk(:);
f3 = figure('Name','Magnetic Horizontal Component Versus Time Series');
f3.WindowState='maximized';
for k = 1:nfiles
subplot(2,2,k);
plot(wak, DOH, 'Color', 'g', 'LineWidth',1.1);
title(sprintf('Data %s',tgl(k)), 'fontsize', 11, 'fontweight','bold');
xlabel('Time Series','fontweight','bold','fontsize',10);
ylabel('Magnetic Horizontal Component Lemi Non IAGA Data (nT)','fontweight','bold','fontsize',10);
legend('off');
grid on
end
end
end
However, by using the above code it would result the wrong output or maybe error.... I just want to set plot title for each subplot (2,2,k) by using title i get from each of multiselect file data (tgl variable). And the subplot each plot wak variable versus DOHk variable which re get from each of multiselect file data.
Is that possible to do so? Would anyone land me a hand to find the solution of the above code i made before? Im so grateful for your helpfulness here.... Thanks.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 12 Aug 2021
You have a simple bug - both your for-loops uses the same loop-variable, k. That's the cause of your problem. My method to avoid this problem is to use "numbered" indices, i1, i2, i3 if they are used to index matrices I make sure to match the number to the matrix-dimension, otherwise I simply count up the number for the inner loops, this helps me avoid doing this anymore. My suggestion is that you do the same, and change k in the outer loop to k1 and in the inner loop to k2.
HTH

Community Treasure Hunt

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

Start Hunting!