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 Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by