Load file with certain number

Dear all,
I'm writing a program in Matlab, where all data is first loaded from the file '20.mat'. Then I create a vector with all 20's, to remember the data in that column is from the film '20.mat'. After some scripts, I want to replace one of these column with data from a file-number lower. So for example:
First: A=[20 20 20 20 20]
Replacing place: 4
Load data with number from A(4)-1 = 19, and replace column 4 with the data from column 4 of '19.mat'.
The problem however is that I don't know beforehand with number I have to load, because if in the next step the replacing place is 1, I again need to load data from '19.mat', but if I again have to replace place 4, I have to load '18.mat'. I could write an if-loop for all separate cases, but because there are quite a lot of cases, I was wondering if there is a direct way to do this. Something like load('r.mat'), where r=A(4)-1.
Kind regards, Marjolein

 Accepted Answer

r=sprintf('%d',A(4)-1)
data=load(r)

2 Comments

Marjolein commented
Thank you for your response. While implementing the above, I realize I have to both load '19.mat' and 'd19.mat'. It is probably easy to add the 'd' somewhere, but I haven't really found it yet. Can you help me?
Marjolein commented
Nevermind, I already got it!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!