Borrar filtros
Borrar filtros

Repeated string what been increment by 1?

3 visualizaciones (últimos 30 días)
Hesham Ismail
Hesham Ismail el 28 de Jul. de 2015
Comentada: Hesham Ismail el 28 de Jul. de 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.

Respuesta aceptada

Cedric
Cedric el 28 de Jul. de 2015
Editada: Cedric el 28 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by