Remake a single file data plot to multiselect file data plot script

조회 수: 1(최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 7월 23일
댓글: Tyann Hardyn 2021년 8월 9일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Simon Chan
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
Tyann Hardyn
Tyann Hardyn 2021년 8월 9일
Thats great, Sir Simon Chan. Awesome....
Hey, i just wonder how to reshape your 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
To become vertical output like this :
Date A 00:01:00 392267.00 -91787.00
Date A 00:02:00 392267.00 -91787.00
Date B 00:01:00 392602.00 -91798.00
Date B 00:02:00 392603.00 -91798.00
Date C 00:01:00 392366.00 -91693.00
Date C 00:02:00 392377.00 -91700.00
Date D 00:01:00 392635.00 -91751.00
Date D 00:02:00 392639.00 -91751.00
Because as you mention at above output i made, it should be placed by date data also so it can be plotted well, Sir....

댓글을 달려면 로그인하십시오.

추가 답변(1개)

KSSV
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
Tyann Hardyn
Tyann Hardyn 2021년 7월 24일
by using the above code i write, it didnt work, Sir. It didnt run to open a file, thought. To open a file (.txt) i want, it should to use uiget...

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by