I have 500 matrices named A1B'1....20'C'1...25' in mat format. I need to make them look like A1BiCj( where i=1:20 and j=1:25) to do function on them. does anyone knows how I can do it?!

8 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 12 Juil 2016
What is the logic of this A1B'1....20'C'1...25' ?
Walter Roberson
Walter Roberson le 12 Juil 2016
Which MATLAB release are you using? In newer versions of MATLAB it is more difficult to fix corrupted variable names (apostrophe is not valid in variable names in MATLAB.)
Walter Roberson
Walter Roberson le 12 Juil 2016
Are you using multiple files, or are you using multiple variables stored within one .mat file?
If you are using multiple files, then is the variable name the same in each file but the file names tell you which case it is?
If you are using multiple variables in the same file, please give at least two different existing variable names and the corresponding names you want them to become.
Walter Roberson
Walter Roberson le 12 Juil 2016
Please give at least two sample variable names inside the one .mat file, and please indicate the names you want them to become. At the moment I do not see any difference between the variable names you already have and the variable names that you want to have.
Konark Kelaiya
Konark Kelaiya le 12 Juil 2016
Modifié(e) : Konark Kelaiya le 12 Juil 2016
Hello
Your question is still not clear.Is it you have 500 variable stored in MAT file and all variables are A1 A2 B1 B2 C1 C2 and so on?
In this case you can use var = load('testMat.mat') ; % testMAT.mat is your MAT file
This will create a single variable as structure containing all variables from MAT file
Then after you can use concatenation to get data
data{cnt} = [var.A1 var.(['B' num2str(i)]) var.(['C' num2str(j)]];
wherein data is {}(cell) and cnt is counter ; i and j is your loops for B and C variable
Rena Berman
Rena Berman le 11 Juin 2019
(Answers Dev) Restored edit

Connectez-vous pour commenter.

 Réponse acceptée

Thorsten
Thorsten le 12 Juil 2016
load yourmatfile.mat
for i = 1:20
for j = 1:25
eval(['A1(:,:,' int2str(i) ',' int2str(j) ') = A1B' int2str(i) 'C' int2str(j) ';'])
end
end
Now you can access A1B10C13 as
A1(:,:,10,13)

3 commentaires

Stephen23
Stephen23 le 12 Juil 2016
Modifié(e) : Stephen23 le 12 Juil 2016
"you can access A1B10C13 as"
A1(:,:,10,13)
All 500 of them in loops ? How ? Hopefully not by using more bad code practices to "fix" this bad code:
Thorsten
Thorsten le 12 Juil 2016
No, just the names. Use the approach I outlined above.
Stephen23
Stephen23 le 12 Sep 2023
Modifié(e) : Stephen23 le 12 Sep 2023
Note that the EVAL documentation recommends only calling EVAL on the RHS if possible:
which also makes this answer simpler:
for i = 1:20
for j = 1:25
A1(:,:,i,j) = eval(['A1B',int2str(i),'C',int2str(j)]);
end
end
Even better would be to avoid the situation by LOADing into output variables.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by