Plotting several .txt files

조회 수: 12 (최근 30일)
Feliciano Döring
Feliciano Döring 2020년 7월 31일
편집: Feliciano Döring 2020년 8월 1일
I want to read several .txt files and plot the 4th and 5th columns of each of them in just one grap. I tried adapting the code from Here but the graph appears blank. IF it would be possible, I would also like to put and identification on each 2D line. Here is what I got so far,
myFolder = 'Users\adminstrator\Desktop\Curves';
filePattern = fullfile(myFolder, '*.txt');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
plot(fullFileName(:,4),fullFileName(:,5));
hold on
end
hold off

채택된 답변

dpb
dpb 2020년 7월 31일
...
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
plot(fullFileName(:,4),fullFileName(:,5));
...
You created a fully-qualified file name, but you never used it to read the file.
Your plot() command uses the filenames as variables for x,y but those are the names of the files, the name doesn't contain the data in the file; you have to read it some way.
The code link you used as a pattern was looking at .png files so it used
...
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
...
to read and display the image in the file. Your simple text file format with no header line will be easy with
...
fullFileName = fullfile(theFiles(k).folder, baseFileName);
disp(['Now reading ', fullFileName])
data=readmatrix(fullFileName);
plot(data(:,4),data(:,5));
...
As written, each loop will replace the preceding line with the next; you're missing a key element to plot all on one graph--"hold". Read documenation for it to get the bigger picture...
You could also streamline the sample code a little; it was written to make every step obvious which isn't all bad...
myFolder = 'Users\adminstrator\Desktop\Curves';
d=dir(fullfile(myFolder, '*.txt'));
figure, hold on
for k = 1:numel(d)
data=readmatrix(fullfile(d(k).folder,d(k).name);
plot(data(:,4),data(:,5));
end
  댓글 수: 5
dpb
dpb 2020년 8월 1일
Typo? There's a reason but we can't see your terminal from here...what does
pwd
return?
Don't need a drive letter by any chance?
If the code and files are in same directory, then you should be able to just use
d=dir('*.txt');
Does that return expected result at command line?
Feliciano Döring
Feliciano Döring 2020년 8월 1일
편집: Feliciano Döring 2020년 8월 1일
Yes, I forgot the drive, thanks! And substituting
d=dir(fullfile(myFolder, '*.txt'));
for
d=dir('*.txt');
yielded good results, the only problem is my version of Matlab is 2015a, and as I researched the folder field was added only in 2016a, so that is why it is not working. But thanks!
Edit:I substituted the
data=readmatrix(fullfile(d(k).folder,d(k).name);
with
data=dlmread(myFolder,d(k).name);
And it worked.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by