Remake a single file data plot to multiselect file data plot script
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Stephen23
2021년 7월 25일
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.
채택된 답변
Simon Chan
2021년 7월 27일
Slightly modify your coed as follows:
(1) Select multiple files via uigetfile
(2) Variable 'full' becomes a cell array now
(3) No need to read the file the second time and create variable C, directly retrieve the data from variable B
(4) Modify the title of the figure
(5) You need to edit a new txt file name each time, otherwise it will be overwritten every time in the loop
clear; clc;
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet LEMI IMF','Multiselect','on');
full = fullfile(direktori,namafile);
nfiles = size(full,2);
for k = 1:nfiles
B = readmatrix(full{k});
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
imfz1 = B(1:2:end,3); % Extract odd number of rows at column 3
imfz2 = B(1:2:end,7); % Extract odd number of rows at column 7
imfz3 = B(2:2:end,3); % Extract even number of rows at column 3
imfz4 = B(2:2:end,7); % Extract even number of rows at column 7
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
%
[savefile, direct, default] = uiputfile('*.txt', 'Save As', 'Horizontal & Vertical Magnetic Component');
%eval(['cd ''' direct ''';']);
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);
end
댓글 수: 4
Simon Chan
2021년 8월 2일
Once again, slightly modify the previous code and note the following:
(1) Use subplot after the for loop to plot the result on one figure. However, if you have thousands of similar files, this method is not suitable for you. So please decide yourself.
(2) For saving the result in one txt file, I am not sure what kind of format you want and hence just give you an example as follows. You may modify by yourself.
clear; clc;
[namafile,direktori]=uigetfile('*.txt','Load Data Magnet LEMI IMF','Multiselect','on');
full = fullfile(direktori,namafile);
nfiles = size(full,2);
for k = 1:nfiles
B = readmatrix(full{k});
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_combine(:,k) = imfh(:);
%Reading Vertical Magnetic Component by using readmatrix
imfz1 = B(1:2:end,3); % Extract odd number of rows at column 3
imfz2 = B(1:2:end,7); % Extract odd number of rows at column 7
imfz3 = B(2:2:end,3); % Extract even number of rows at column 3
imfz4 = B(2:2:end,7); % Extract even number of rows at column 7
imfz=[imfz1, imfz2, imfz3, imfz4]';
imfz_combine(:,k) = imfz(:);
end
%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');
f1 = figure('Name','Magnetic Horizontal Component Versus Time Series');
f1.WindowState='maximized';
for k = 1:nfiles
subplot(2,2,k)
plot(time, imfh_combine(:,k), 'Color', 'g', 'LineWidth',1.1);
title(sprintf('th for file %d',k), '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
end
f2 = figure('Name','Magnetic Vertical Component Versus Time Series');
f2.WindowState='maximized';
for k = 1:nfiles
subplot(2,2,k)
plot(time, imfz_combine(:,k), 'Color', 'b', 'LineWidth',1.1);
title(sprintf('th for file %d',k), '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
end
%
output_combine = vertcat(imfh_combine,imfz_combine);
output_combine = reshape(output_combine(:),[],nfiles*2);
format = ['%s ', repmat('%.2f ',1,nfiles*2), '\n'];
[savefile, direct, default] = uiputfile('*.txt', 'Save As', 'Horizontal & Vertical Magnetic Component');
cd(direct);
fout=fopen(savefile,'w');
for i = 1:length(time)
fprintf(fout,format,time(i),output_combine(i,:));
end
fclose(fout)
Text file output:
00:01:00 392267.00 -91787.00 392602.00 -91798.00 392366.00 -91693.00 392635.00 -91751.00
00:02:00 392267.00 -91787.00 392603.00 -91798.00 392377.00 -91700.00 392639.00 -91751.00
00:03:00 392266.00 -91785.00 392605.00 -91800.00 392372.00 -91698.00 392641.00 -91750.00
00:04:00 392264.00 -91784.00 392608.00 -91801.00 392373.00 -91693.00 392644.00 -91751.00
.....
추가 답변 (1개)
KSSV
2021년 7월 23일
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
KSSV
2021년 7월 23일
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?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!