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
0 个评论
采纳的回答
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
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!