Getting around overuse of "if" statements.
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a function which takes some data and adds it to an excel file. Depending on the inputs to the function, the data is put in different rows and columns of the excel file. The input is the day, week and site the data was recorded. My code is very clunky and just involves lots of "if" and elseif" statements which make the code hard to read and it takes ages to fix the code if something goes wrong. Is there a better way to write the following code?
function FileLocation(Data,Diff,DAY,SITE,WEEK)
if SITE == 'A'
if DAY == 'Mon'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','C10:C14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','D10:D14')
elseif DAY == 'Tue'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','E10:E14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','F10:F14')
elseif DAY == 'Wed'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','G10:G14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','H10:H14')
elseif DAY == 'Thu'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','I10:I14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','J10:J14')
else
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','K10:K14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','L10:L14')
end
end
0 Commenti
Risposta accettata
Rik
il 13 Lug 2021
Modificato: Rik
il 13 Lug 2021
You should use the value of DAY to set the value of a variable you can use as the range. You should also use strcmp to compare char arrays.
In this case you can use ismember:
function FileLocation(Data,Diff,DAY,SITE,WEEK)
if strcmp(SITE,'A')
filename = 'daily records.xlsx';
days={'Mon','Tue','Wed','Thu','Fri'};
LookUpTable={...
'C10:C14','D10:D14';...
'E10:E14','F10:F14';...
'G10:G14','H10:H14';...
'I10:I14','J10:J14';...
'K10:K14','L10:L14'};
L=ismember(days,DAY);
%you could check if sum(L) is 1 here if you like
Range=LookUpTable(L,:);
writematrix(Data,filename,'Sheet',WEEK,'Range',Range{1});
writematrix(Diff,filename,'Sheet',WEEK,'Range',Range{2});
end
The point is to make sure you are not repeating code. If you have code that you are calling mutliple times, you should either consider a loop or a function. A LUT can also be helpful to avoid repeating code.
5 Commenti
Rik
il 13 Lug 2021
That's fine as well. You need to balance the chance of making a mistake when copy-pasting the LUT with the chance of making a mistake when writing the code that will generate the LUT. Case in point: my initial answer had 'I10:I14','I10:I14' instead of 'I10:I14','J10:J14'.
Più risposte (2)
Srinik Ramayapally
il 13 Lug 2021
Hey Jacob,
You can always use a switch-case instead of multiple if/else-if statements.
Here in this case, instead of the inner if/else-if statements, i suggest you to replace that code with this
switch DAY
case 'Mon'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','C10:C14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','D10:D14')
case 'Tue'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','E10:E14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','F10:F14')
case 'Wed'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','G10:G14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','H10:H14')
case 'Thu'
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','I10:I14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','J10:J14')
otherwise
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range','K10:K14')
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range','L10:L14')
end
the case element can also accept multiple values like strings, cells etc.
Max Heiken
il 13 Lug 2021
I agree with Srinik Ramayapally, employing switch is the most obvious change that comes to mind. To extend on that solution, you could factor out the code common to all cases.
switch DAY
case 'Mon'
range1 = 'C10:C14';
range2 = 'D10:D14';
case 'Tue'
range1 = 'E10:E14';
range2 = 'F10:F14';
case 'Wed'
range1 = 'G10:G14';
range2 = 'H10:H14';
case 'Thu'
range1 = 'I10:I14';
range2 = 'J10:J14';
otherwise
range1 = 'K10:K14';
range2 = 'L10:L14';
end
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range',range1)
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range',range2)
To completely go without a switch, you could employ a Map object.
range1 = containers.Map({'Mon','Tue','Wed','Thu','Fri'},{'C10:C14','E10:E14','G10:G14','I10:I14','K10:K14'});
range2 = containers.Map({'Mon','Tue','Wed','Thu','Fri'},{'D10:D14','F10:F14','H10:H14','J10:J14','L10:L14'});
filename = 'daily records.xlsx';
writematrix(Data,filename,'Sheet',WEEK,'Range',range1(DAY))
filename = 'daily records.xlsx';
writematrix(Diff,filename,'Sheet',WEEK,'Range',range2(DAY))
But notice that I had to assume here that "otherwise" always means "Fri". You could add more keys into the map that point to 'K10:K14'.
Vedere anche
Categorie
Scopri di più su Data Import from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!