sequential file reading and operations

2 vues (au cours des 30 derniers jours)
PIYUSH MOHANTY
PIYUSH MOHANTY le 3 Avr 2019
Commenté : Rik le 4 Avr 2019
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 commentaires
Stephen23
Stephen23 le 4 Avr 2019
Modifié(e) : Stephen23 le 4 Avr 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 le 4 Avr 2019
Thanks Stephen for your prompt reply.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 3 Avr 2019
Modifié(e) : Rik le 4 Avr 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 commentaires
PIYUSH MOHANTY
PIYUSH MOHANTY le 4 Avr 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 le 4 Avr 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by