Repeated string what been increment by 1?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Cedric
2015-7-28
编辑:Cedric
2015-7-28
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!