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

조회 수: 1 (최근 30일)
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

답변 (1개)

Tejas
Tejas 2024년 11월 26일
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:

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by