There are 50 dat files in a folder. I want to import it to matlab and then select specific dat files.

1 visualización (últimos 30 días)
There are 50 dat files in a folder. I want to import it to matlab and then select specific dat files say ( 8.dat, 15.dat, 18.dat and 36.dat ). And i want to compare and plot datas from these selected 4 dat files.
For eg, read all the columns of 8.data, 15.dat, 18.dat and 36.dat
plot ( 8.datas 1st column with 2nd column ) hold on (15.datas 1st column with 2nd column) hold on (18.datas 1st column with 2nd) hold on (36.datas 1st column with 2nd) hold off
second plot,
plot( 8.datas 1st column with 3rdcolumn ) hold on (15.datas 1st column with 3rd column) hold on (18.datas 1st column with 3rd hold on (36.datas 1st column with 3rd ) hold off
end

Respuestas (1)

Tejas
Tejas el 26 de Nov. de 2024
Hello Shrutidat,
To plot the contents of .DAT files according to the requirements mentioned above, follow these steps:
  • Add the names of the files you want to load into a cell array.
folderPath = 'sample_data';
fileNames = {'8.dat', '15.dat', '18.dat', '36.dat'};
  • Assuming the data in the .DAT files is in matrix format, use the code snippet below to import them into MATLAB.
data = cell(length(fileNames), 1);
for i = 1:length(fileNames)
filePath = fullfile(folderPath, fileNames{i});
data{i} = readmatrix(filePath);
end
  • Use the provided code snippet to plot the data.
% Plot the data
figure;
% First plot: 1st column vs 2nd column
subplot(2, 1, 1); % Create a subplot for the first plot
hold on;
for i = 1:length(data)
plot(data{i}(:, 1), data{i}(:, 2), 'DisplayName', fileNames{i});
end
hold off;
title('1st Column vs 2nd Column');
xlabel('1st Column');
ylabel('2nd Column');
legend show;
% Second plot: 1st column vs 3rd column
subplot(2, 1, 2); % Create a subplot for the second plot
hold on;
for i = 1:length(data)
plot(data{i}(:, 1), data{i}(:, 3), 'DisplayName', fileNames{i});
end
hold off;
title('1st Column vs 3rd Column');
xlabel('1st Column');
ylabel('3rd Column');
legend show;
For a better understanding of the solution, refer to these documentations:

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by