Plotting several .txt files

15 visualizaciones (últimos 30 días)
Feliciano Döring
Feliciano Döring el 31 de Jul. de 2020
Editada: Feliciano Döring el 1 de Ag. de 2020
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

Respuesta aceptada

dpb
dpb el 31 de Jul. de 2020
...
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 comentarios
dpb
dpb el 1 de Ag. de 2020
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 el 1 de Ag. de 2020
Editada: Feliciano Döring el 1 de Ag. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by