Filter löschen
Filter löschen

Repeated string what been increment by 1?

3 Ansichten (letzte 30 Tage)
Hesham Ismail
Hesham Ismail am 28 Jul. 2015
Kommentiert: Hesham Ismail am 28 Jul. 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.

Akzeptierte Antwort

Cedric
Cedric am 28 Jul. 2015
Bearbeitet: Cedric am 28 Jul. 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.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by