필터 지우기
필터 지우기

replace dlmread file name with a variable in a loop

조회 수: 5 (최근 30일)
Avik Mahata
Avik Mahata 2022년 6월 12일
답변: Avik Mahata 2022년 6월 12일
I have around 20 files needed to be read and plotted. So the file names are such as file.288.10 , file.288.100 etc. I have 20 files. I am tryng in the following code, but its not reading T and P.
for T = [288 299 308 323 337]
for P = [10 100 340 544]
M = dlmread('file.T.P', ' ', 1, 0);
figure
plot (M(:,1), M(:,2))
hold on
plot (M(:,1), M(:,3))
hold on
end
end

채택된 답변

Image Analyst
Image Analyst 2022년 6월 12일
Maybe this:
folder = pwd; % Or wherever you want
for T = [288 299 308 323 337]
for P = [10 100 340 544]
baseFileName = sprintf('file.%d.%d.csv', T, P); % Change extension if it's not csv.
fullFileName = fullfile(folder, baseFileName);
if isfile(fullFileName)
M = readmatrix(fullFileName);
figure
plot (M(:,1), M(:,2), 'b-', 'LineWidth', 2)
grid on;
hold on
plot (M(:,1), M(:,3), 'r-', 'LineWidth', 2)
legend('Column 2', 'Column 3');
title(baseFileName, 'FontSize', 18)
else
fprintf('Warning: file not found : "%s".\n', fullFileName)
end
end
end
If that doesn't work, attach two of your files in a zip file after reading this:

추가 답변 (2개)

dpb
dpb 2022년 6월 12일
You've got to turn the numerics into string filename to build dynamically --
for T = [288 299 308 323 337]
for P = [10 100 340 544]
fname=sprintf('file%d.%d',T,P);
M = dlmread(fname,' ', 1, 0);
figure
plot (M(:,1), M(:,2:3))
end
end
Unless you've an older release of MATLAB, I'd suggest using readmatrix instead of dlmread

Avik Mahata
Avik Mahata 2022년 6월 12일
Thanks for the reply. Both works.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by