If statement Alternative for listdlg Index?
3 views (last 30 days)
Show older comments
Hi all,
I'm not the most efficient programmer by any stretch, and I am currently trying to run the code below with a bit more efficiency. I feel like there must be an alternative to using an 'if statement' in the code I've attached below.
Does anybody know if could use the index value given from lisdl to minus from the areas without using numerous if statements?
I'm only asking because there is quite a few other variables in need to process in this way and i feel like i'm going to end up with such a long script.
Any help would be so greatly appreciated.
Thanks!!
x = inputdlg({'Area of Window 1','Area of Window 2','Area of Window 3','Area of Window 2'},...
'Areas of Windows');
Window1 = str2num(x{1});
Window2 = str2num(x{2});
Window3 = str2num(x{3});
Window4 = str2num(x{4});
[W1] = listdlg('PromptString','What wall is Window 1 on?:',...
'SelectionMode','single',...
'ListString',OOL);
[W2] = listdlg('PromptString','What wall is Window 1 on?:',...
'SelectionMode','single',...
'ListString',OOL);
NFA = RL*RH;
SFA = RL*RH;
EFA = RW*RH;
WFA = RW*RH;
if W1 == 1
NFA = RL*RH-Window1;
elseif W1==2
SFA = RL*RH-Window1;
elseif W1==3
EFA = RW*RH-Window1;
else
WFA = RW*RH-Window1;
end
if W2 == 1
NFA = RL*RH-Window2;
elseif W1==2
SFA = RL*RH-Window2;
elseif W2==3
EFA = RW*RH-Window2;
else
WFA = RW*RH-Window2;
end
0 Comments
Answers (1)
dpb
on 24 Mar 2017
Sure, that's what switch blocks are for...
switch W1
case 1
NFA = RL*RH-Window1;
case 2
SFA = RL*RH-Window1;
case 3
EFA = RW*RH-Window1;
otherwise
WFA = RW*RH-Window1;
end
It's possible (probable?) that you could also cut down the cases as well and use arrays or other ways to keep track of conditions rather than separately-named variables. A categorical variable or the like could be an option as you've got only two separate numerically-distinct cases above with just some id difference. That could be a variable instead of a different variable and likely simplify coding later on by being able to reuse code with the different flag variable value instead of having to duplicate it to use the different variables as you now have.
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!