sequential file reading and operations

조회 수: 2 (최근 30일)
PIYUSH MOHANTY
PIYUSH MOHANTY 2019년 4월 3일
댓글: Rik 2019년 4월 4일
I need to read the file PPT1.txt, PPT2.txt,PPT3.txt present in 'D:\IEM\TEST1-IEM\Analysis of results\0.25g\PPT\Time Vs Pore Pressure path. The file has two columns of float values. The following for loop in the code does not throw error, but does not do the job. Can you please help?
for i =1:3
rootdir= 'D:\\IEM\\TEST1-IEM\\Analysis of results\\0.25g\\PPT\\Time Vs Pore Pressure\\';
fid = sprintf([rootdir '%d.txt'],i);
T = textscan(fid,'%f %f');
% x=T{1};
% y=T{2};
% plot (x,y);
% xlabel('Time');
% ylabel('Pore Pressure');
% legend('PPT1','location','northwest');
end
  댓글 수: 2
Stephen23
Stephen23 2019년 4월 4일
편집: Stephen23 2019년 4월 4일
There is no need to define rootdir on every iteration, it is always the same: just define it before the loop. It is recommended to use fullfile to join paths and filenames, as it correctly handles the file separator character:
D = 'D:\IEM\TEST1-IEM\Analysis of results\0.25g\PPT\Time Vs Pore Pressure';
for k = 1:3
F = sprintf('PPT%d.txt',k);
[fid,msg] = fopen(fullfile(D,F),'rt');
assert(fid>=3,msg)
... import file data
fclose(fid);
... your code
end
PIYUSH MOHANTY
PIYUSH MOHANTY 2019년 4월 4일
Thanks Stephen for your prompt reply.

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

채택된 답변

Rik
Rik 2019년 4월 3일
편집: Rik 2019년 4월 4일
That is not how the textscan function works. You should read the documentation for the functions you are using.
figure(1)
rootdir= 'D:\IEM\TEST1-IEM\Analysis of results\0.25g\PPT\Time Vs Pore Pressure\';
for i =1:3
fname = sprintf('%sPPT%d.txt',rootdir,i);%don't forget the PPT in the file name
fid=fopen(fname);
T = textscan(fid,'%f %f');
fclose(fid);
x=T{1};
y=T{2};
subplot(1,3,i)
plot (x,y);
xlabel('Time');
ylabel('Pore Pressure');
legend(sprintf('PPT%d',i),'location','northwest');
end
  댓글 수: 2
PIYUSH MOHANTY
PIYUSH MOHANTY 2019년 4월 4일
Thanks Rik, It worked.
Couple of questions:
1)I am a bit new to cell array. How does the matlab understand that T{1} is same as T{1,1} or T{2} is same as T{1,2}?
2) I want to have more idea regading the sprintf function as I see it to be good tool to read values from the files and I need to analyst a cvast amount of text files. Can you please guide me in that direction as in Matlab Documentation, I do not see any links where I can make use iof directory inside the sprintf function.
Rik
Rik 2019년 4월 4일
1) if you are only using a single index, Matlab will treat that as a linear index. For vectors there is no difference, but for matrices there is. You can play around with something like Test=[-1 -2;1 2];disp(Test(2)) to see how it works for arrays.
2) you can use sprintf to create strings (or technically speaking char arrays). In this case that means that you can use it to generate the filename, so you can use fopen to generate a file identifier that textscan can use. So apart from suggesting you read the doc pages for sprintf and fopen I don't know what I should suggest.
If my answer solved your issue, please consider marking it as accepted answer. If not, feel free to comment with your remaining issues.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by