How to read txt files with variables in the file name?

조회 수: 2 (최근 30일)
johan
johan 2019년 1월 7일
댓글: johan 2019년 1월 8일
Dear Matlab users,
Maybe someone can help me with the following:
For a project I have file names of the following form: abc@_def#_ghi$.txt where the @, # and $ are variables. These variables are known. E.g. the @-variable=[1 2 3 4 5], #-variable=[0.1 0.2 0.6] and $-variable=[40 50 60 70 80 90] (which makes 5*3*6=90 files in total).
If I would like to read the file with: @=1, #=0.6 and $=70, I use the following:
outcome=dlmread('abc1_def0.6_ghi70.txt','\t') %Note: \t, because columns are seperated by tabs
Now I would like to know how I can read these txt-files in the following way:
outcome=dlmread('abc@(1)_def#(3)_ghi$(4).txt','\t')
with: @(1)=1, #(3)=0.6 and $(4)=70.
In other words, I would like to use the vectors to define the name of the file. Is there a method to achieve this?
Thank you.

채택된 답변

Jan
Jan 2019년 1월 7일
% Replace "p1, p2, p3" by smarter names...
p1 = [1 2 3 4 5];
p2 = [0.1 0.2 0.6];
p3 = [40 50 60 70 80 90];
for i1 = p1
for i2 = p2
for i3 = p3
filename = sprintf('abc%d_def%.1f_ghi%02d.txt', i1, i2, i3)
...
end
end
end

추가 답변 (1개)

johan
johan 2019년 1월 8일
Dear Jan,
Thank you for your fast response. Problem solved!!
Regards, Johan

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by