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

1 view (last 30 days)
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.

Accepted Answer

Jan
Jan on 7 Jan 2019
% 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

More Answers (1)

johan
johan on 8 Jan 2019
Dear Jan,
Thank you for your fast response. Problem solved!!
Regards, Johan

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!