sequential file reading and operations

1 view (last 30 days)
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 Comments
Stephen23
Stephen23 on 4 Apr 2019
Edited: Stephen23 on 4 Apr 2019
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 on 4 Apr 2019
Thanks Stephen for your prompt reply.

Sign in to comment.

Accepted Answer

Rik
Rik on 3 Apr 2019
Edited: Rik on 4 Apr 2019
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 Comments
PIYUSH MOHANTY
PIYUSH MOHANTY on 4 Apr 2019
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 on 4 Apr 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!