Multiple .txt input files

조회 수: 2 (최근 30일)
Ironmaniac
Ironmaniac 2017년 2월 23일
댓글: Ironmaniac 2017년 2월 24일
Hello,
I have created a code as shown
data1=importdata('1.txt');
phase=data1(:,3);
p=phase*2*pi;
t=data1(:,1);
fd=diff(p);
sd=diff(fd);
ti=(t/86400000);
tii = fix(ti);
tiii = (ti - tii)*24 ;
tj=tiii;
tj(end,:) = [];
tk=tj;
tk(end,:) = [];
FigHandle = figure('Position', [100, 100, 560, 751]);
ax1=subplot(3,1,1);
plot(ax1,tiii,p)
title( ' 1 Oct 2012, SV 1 ')
xlabel('UT (hours)')
ylabel('Phase at L1 (cycles)')
ax2=subplot(3,1,2);
plot(ax2,tk,sd)
xlabel('UT (hours)')
ylabel('sec diff on Phase at L1 (cycles)')
ax3=subplot(3,1,3);
plot(ax3,tk,sd)
xlabel('UT (hours)')
ylabel('sec diff on Phase at L1 (cycles)')
axis(ax3,[0 24 -2 2])
axis(ax2,[-inf inf -2 2])
axis(ax1,[-inf inf -inf inf])
This code runs on input text files with structured numerical data to do a calculation and generate a figure with the results. How can I run the program on multiple input files (say, 1.txt , 2.txt, 3.txt, 16.txt, 27.txt etc)all at once and get a plot for each input?
Please help.
PS:I am attaching two input files 10.txt and 27.txt

채택된 답변

Massimo Zanetti
Massimo Zanetti 2017년 2월 23일
편집: Massimo Zanetti 2017년 2월 23일
Use dir function to list files with .txt extension
%list folder files
files = dir('*.txt');
and put your code in a function
function myFunction(fname)
%%your code....
end
and run the function for every file in your folder using a for loop Like this:
for k=1:length(files)
%get file name
fname = files(k).name;
%run your code on the current file
myFunction(fname);
end
  댓글 수: 3
Massimo Zanetti
Massimo Zanetti 2017년 2월 23일
편집: Massimo Zanetti 2017년 2월 23일
Play around this (in the body of myFunction):
function myFunction(fname)
%%your code....
figure;
plot(something);
title(['file: ',fname]);
end
If this answer helped you, please accept it.
Ironmaniac
Ironmaniac 2017년 2월 24일
Thank you, Mr. Zanetti.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by