Effacer les filtres
Effacer les filtres

Repeated string what been increment by 1?

3 vues (au cours des 30 derniers jours)
Hesham Ismail
Hesham Ismail le 28 Juil 2015
Commenté : Hesham Ismail le 28 Juil 2015
Hello,
I have this code below that have a lot of repeated parts which are string
Any idea how to improve and make it more denser
for i= 1: 10
if i==6
if exist('A_6', 'file') == 2
load A_6;
else
run Test
end
elseif i==7
if exist('A_7.mat', 'file') == 2
load A_7;
else
run Test
end
elseif i==8
if exist('A_8.mat', 'file') == 2
load A_8;
else
run Test
end
elseif i==9
if exist('A_9.mat', 'file') == 2
load A_9;
else
run Test
end
elseif i==10
if exist('A_10.mat', 'file') == 2
load A_10;
else
run Test
end
end
end
It is basically check if file is available, if the file is available load it otherwise run the Test file to get the values.

Réponse acceptée

Cedric
Cedric le 28 Juil 2015
Modifié(e) : Cedric le 28 Juil 2015
The approach is questionable, but let's say that technically you can do this:
for k = 6 : 10
baseName = sprintf( 'A_%d', k ) ;
if exist( [baseName, '.mat'], 'file' )
load( baseName ) ;
else
run Test
end
end
PS: I used k instead of i, because we usually keep i and j for complex numbers. If your script Test uses i from the workspace though (which would not be a good practice), you will have to either use i as a loop index, or update the script so it uses k.
  1 commentaire
Hesham Ismail
Hesham Ismail le 28 Juil 2015
Thanks that made my code more cleaner

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by