Plotting several .txt files

8 次查看(过去 30 天)
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
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 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by