sequential file reading and operations

2 次查看(过去 30 天)
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

请先登录,再进行评论。

采纳的回答

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