how to read lots of file and plot them
13 次查看(过去 30 天)
显示 更早的评论
Hi
Dearly need your help!
I have a folder,its name is "waves".
Files name inside it are like
S(numbers from 1 to 54).L0.FX(X Y Z) for example ⇒ S46.L0.FXZ.sema
so there are 54*3=162 files in my "waves" folder.
each file has 2 columns of data, time and acceleration.
I have to read all the files for a same number and 3 X,Y,Z component and plot them in one figure
S(number).L0.FX(X).sema
S(number).L0.FX(Y).sema
S(number).L0.FX(Z).sema
I suppose to have 54 figurs.
it is just too complicated
how can i write a code that is going to read data and plot, I have no idea.
the only thing that I came up with
idir='./waves/'
files=dir([idir.'*sema'])
for i=1:numel(files)/3;
for d=[X Y Z]
file_name = ............
end
please make it simple I am still studying
Thank You
2 个评论
采纳的回答
Raj
2019-7-9
You can use this:
for ii=1:54 % Number of S set of files
for jj=1:3 % Number of sub files for each S index
if jj==1
temp='X';
elseif jj==2
temp='Y';
else
temp='Z';
end
%% Initialize variables.
myfilename= sprintf('S%i.L0.FX%s.sema',ii,temp);
filename = fullfile('C:\','Users','user','Documents','MATLAB','waves_sample',myfilename);% Put your file path here
delimiter = ' ';
%% Format string for each line of text:
% column1: double (%f)
% column2: double (%f)
formatSpec = '%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names (NOTE: Time and Acceleration data gets overwritten with each new file read)
Time = dataArray{:, 1};
Acceleration = dataArray{:, 2};
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
figure(ii)% Figure1 will be for S1 three files and so on
plot(Time,Acceleration)% customize plots here as you desire
hold on
end
end
2 个评论
ddd ppp
2020-4-25
did you just make that? or did you find that code somewhere? thats a lot of code for one answer. thanks for your time.
更多回答(2 个)
KSSV
2019-7-9
files = dir('*.txt') % Give your extension
N = length(files) ; % total number of files
% loop for each file
for i = 1:N
thisfile = files(i).name ;
% Read the file
% plot the file
end
Endu lake
2022-10-11
%% Import data from text file.
% Script for importing data from the following text file:
%
% /home/endu/Desktop/data/EXBDRIFT_ABJA_CMRN_2018_ONE_MNT/correl^n coff/CNKY_aBN/CNKY_ABAN_DOY_2013.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2022/10/05 09:35:02
%% Initialize variables.
filename = '/home/endu/Desktop/data/EXBDRIFT_ABJA_CMRN_2018_ONE_MNT/correl^n coff/CNKY_aBN/CNKY_ABAN_DOY_2013.txt';
delimiter = ' ';
startRow = 2;
%% Format for each line of text:
% column1: double (%f)
% column2: double (%f)
% column3: double (%f)
% column4: double (%f)
% column5: double (%f)
% column6: double (%f)
% column7: double (%f)
% column8: double (%f)
% column9: double (%f)
% column10: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Allocate imported array to column variable names
Year = dataArray{:, 1};
DOY = dataArray{:, 2};
EEJ = dataArray{:, 3};
ExB = dataArray{:, 4};
IMF = dataArray{:, 5};
Kp = dataArray{:, 6};
Sn = dataArray{:, 7};
Dst = dataArray{:, 8};
ap = dataArray{:, 9};
f10 = dataArray{:, 10};
figure
% % max(EEJ)
% stem(DOY,EEJ,'LineStyle','-.',...
% 'MarkerFaceColor','red',...
% 'MarkerEdgeColor','green')
% hold on
% stem(DOY,IMF,'LineStyle','-.',...
% 'MarkerFaceColor','red',...
% 'MarkerEdgeColor','green')
% h = stem(DOY,EEJ);
% % plot(DOY,EEJ);
% hold on
% plot(DOY,ExB);
% % hold on
% % plot(DOY,IMF);
% hold on
% plot(DOY,Sn);
% hold on
% plot(DOY,Dst);
% hold on
% plot(DOY,f10);
% subplot(511)
% R = corr2(EEJ,ExB)
% b1 = EEJ\ExB
% yCalc1 = b1*EEJ;
% scatter(EEJ,ExB,'LineWidth',3)
% hold on
% plot(EEJ,yCalc1,'LineWidth',3)
% % xlabel('EEJ(nT))','fontsize',10,'fontweight','bold')
% ylabel('ExB (m/s)','fontsize',10,'fontweight','bold')
% title('CNKY, 2013','fontsize',10,'fontweight','bold')
% % legend('Data','Linear regression','Location','best','Orientation','horizontal')
% ax.XColor = 'red';
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% subplot(512)
%
% R = corr2(EEJ,IMF)
% b1 = EEJ\IMF
% yCalc1 = b1*EEJ;
% scatter(EEJ,IMF,'LineWidth',3)
% hold on
% plot(EEJ,yCalc1,'LineWidth',3)
% % xlabel('EEJ(nT))','fontsize',10,'fontweight','bold')
% ylabel('IMF (nT)','fontsize',10,'fontweight','bold')
% % title('2013','fontsize',10,'fontweight','bold')
% % legend('Data','Linear regression','Location','best','Orientation','horizontal')
% % % % % axis off
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% subplot(513)
% R = corr2(EEJ,Sn)
% b1 = EEJ\Sn
% yCalc1 = b1*EEJ;
% scatter(EEJ,Sn,'LineWidth',3)
% hold on
% plot(EEJ,yCalc1,'LineWidth',3)
% % xlabel('IMF (nT))','fontsize',10,'fontweight','bold')
% ylabel('SSN','fontsize',10,'fontweight','bold')
% % title('2013','fontsize',10,'fontweight','bold')
% % legend('Data','Linear regression','Location','best','Orientation','horizontal')
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subplot(514)
% R = corr2(EEJ,Dst)
% b1 = EEJ\Dst
% yCalc1 = b1*EEJ;
% scatter(EEJ,Dst,'LineWidth',3)
% hold on
% plot(EEJ,yCalc1,'LineWidth',3)
% % xlabel('IMF (nT))','fontsize',10,'fontweight','bold')
% ylabel('Dst (nT)','fontsize',10,'fontweight','bold')
% % title('2013','fontsize',10,'fontweight','bold')
% % legend('Data','Linear regression','Location','best','Orientation','horizontal')
%
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subplot(515)
R = corr2(EEJ,f10)
b1 = EEJ\f10
yCalc1 = b1*EEJ;
scatter(EEJ,f10,'LineWidth',3)
hold on
plot(EEJ,yCalc1,'LineWidth',3)
xlabel('EEJ (nT)','fontsize',10,'fontweight','bold')
ylabel('f10.7','fontsize',10,'fontweight','bold')
% title('2013','fontsize',10,'fontweight','bold')
legend('Data','Linear regression','Location','best','Orientation','horizontal')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Language Support 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!