Remake a single file data plot to multiselect file data plot script
Show older comments
Hi everyone. I want to ask about how the way my code should be handled by using multiselect in uigetfile function.... This is my code to plot 2 data columns by using only 1 file :
close all
clear clc
clc
%Getting 1 file contain with IMF Daily data magnet%
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet LEMI IMF');
full = fullfile(direktori,namafile);
%Reading Horizontal Magnetic Component by using readmatrix%
B = readmatrix(full);
idx=isnan(B(:,1)); % Find the index for header rows
B(idx,:)=[]; % Delete the header rows
imfh1 = B(1:2:end,1); % Extract odd number of rows at column 1
imfh2 = B(1:2:end,5); % Extract odd number of rows at column 5
imfh3 = B(2:2:end,1); % Extract even number of rows at column 1
imfh4 = B(2:2:end,5); % Extract even number of rows at column 5
imfh=[imfh1, imfh2, imfh3, imfh4]';
imfh = imfh(:);
%Reading Vertical Magnetic Component by using readmatrix%
C = readmatrix(full);
idx=isnan(C(:,1)); % Find the index for header rows
C(idx,:)=[]; % Delete the header rows
imfz1 = C(1:2:end,3); % Extract odd number of rows at column 1
imfz2 = C(1:2:end,7); % Extract odd number of rows at column 5
imfz3 = C(2:2:end,3); % Extract even number of rows at column 1
imfz4 = C(2:2:end,7); % Extract even number of rows at column 5
imfz=[imfz1, imfz2, imfz3, imfz4]';
imfz = imfz(:);
%Total time series in 1 file (a Daily data magnetic [60 minutes x 24 hours = 1440 minutes])%
m = minutes(1:1:1440);
time = duration(m,'format','hh:mm:ss');
%Plotting Time Series versus Horizontal Magnetic Component%
figure('Name','Magnetic Horizontal Component Versus Time Series')
plot(time, imfh, 'Color', 'g', 'LineWidth',1.1);
title(th, 'fontsize', 14, 'fontweight','bold');
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10);
ylabel('Komponen H Magnet Data Lemi IMF (nT)','fontweight','bold','fontsize',10);
legend('off');
grid on
%Plotting Time Series versus Vertical Magnetic Component%
figure('Name','Magnetic Vertical Component Versus Time Series')
plot(time, imfz, 'Color', 'b', 'LineWidth',1.1);
title(tz, 'fontsize', 14, 'fontweight','bold');
xlabel('Deret Waktu (Time Series)','fontweight','bold','fontsize',10);
ylabel('Komponen Z Magnet Data Lemi IMF (nT)','fontweight','bold','fontsize',10);
legend('off');
grid on
%Saving Time Series, Horizontal Component, and Vertical Component in a
%new txt file
[savefile, direct, default] = uiputfile('*.txt', 'Save As', 'Horizontal & Vertical Magnetic Component');
eval(['cd ''' direct ''';']);
fout=fopen(savefile,'w');
for i=1:length(time)
fprintf(fout,'%s %.2f %.2f\n', time(i), imfh(i), imfz(i));
end
fclose(fout);
The above code is my successful code to plot and save the extracted data with just 1 file without multiselect on code(1 day / daily magnetic data). Now, im need to have it changed from just getting 1 file to become multiselect data. This is the scenario :
1) I have the same kind of data format in a folder (contained with 3 or more data) (i already attaching 3 files with a same type);
2) by using the preliminary code above i can extract the data i want (readmatrix function) from a daily magnetic data (1 file) and get it plot respectively with time series (1:1:1440) so the output is just from 1 file (1 day data) ;
3) I need to sum the 3 of data files (attached files) into an array or table or something like that by using multiselect in uigetfile function so that it can be plotted and saved in a new txt file (just like in my preliminary single file code) <this is my problem>
Would you mind to help me find the solution? Thank you very much /.\
2 Comments
Stephen23
on 25 Jul 2021
Replace this anti-pattern code:
eval(['cd ''' direct ''';']);
with this much simpler code:
cd(direct);
Note: the best approach is to use absolute/relative filenames, rather than changing the current directory.
Tyann Hardyn
on 27 Jul 2021
Accepted Answer
More Answers (1)
KSSV
on 23 Jul 2021
Why you want to use uiget ? You can think of the below option.
txtFiles = dir('*.txt') ;
N = length(txtFiles) ;
for i = 1:N
thisFile = txtFiles(i).name ;
% do what you want
end
You can specify your required strings if you want to read certain files from the folder.
3 Comments
Tyann Hardyn
on 23 Jul 2021
Edited: Tyann Hardyn
on 23 Jul 2021
KSSV
on 23 Jul 2021
Yes do what you want should be replaced with the code which is working for you on single file. Does the given code work for you?
Tyann Hardyn
on 24 Jul 2021
Edited: Tyann Hardyn
on 24 Jul 2021
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!