Best way to convert an Excel model to MATLAB
15 views (last 30 days)
Show older comments
I wish to use MATLAB rather than the following Excel worksheet
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/706042/image.jpeg)
The following MATLAB code acheives this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/706032/image.jpeg)
May I ask is there a better approach than the above, which is sort of like using visual basic in Excel?
For example, is there a more "object orientated" approach where the formulas representing each excel column, e.g. "opening(t) = closing(t-1)", are specified in their own separate file or something? Or, perhaps there is a MATLAB toolbox suitable for this type of model?
Cheers
2 Comments
Answers (1)
Sameer
on 1 Mar 2024
Hi Tim,
For a more complex model like the one you're describing, where you have many columns with interdependent formulas, an object-oriented approach in MATLAB could indeed make your code more manageable and modular. This would involve defining classes that encapsulate the behaviour of your financial model, with methods representing the calculations for each column.
Here's an example of how you might structure such a model using MATLAB's object-oriented programming features:
classdef FinancialModel
properties
Opening
Growth
Contribution
Closing
GrowthRate
ContributionAmount
NumPeriods
end
methods
function obj = FinancialModel(growthRate, contributionAmount, numPeriods)
% Constructor to set up initial values and allocate arrays
obj.GrowthRate = growthRate;
obj.ContributionAmount = contributionAmount;
obj.NumPeriods = numPeriods;
obj.Opening = zeros(1, numPeriods);
obj.Growth = zeros(1, numPeriods);
obj.Contribution = zeros(1, numPeriods);
obj.Closing = zeros(1, numPeriods);
end
function obj = calculatePeriod(obj, t)
% Calculate values for a single period
if t == 1
obj.Opening(t) = 0;
else
obj.Opening(t) = obj.Closing(t-1);
end
obj.Growth(t) = obj.Opening(t) * obj.GrowthRate;
obj.Contribution(t) = obj.ContributionAmount;
obj.Closing(t) = obj.Opening(t) + obj.Growth(t) + obj.Contribution(t);
end
function obj = runModel(obj)
% Run the model for all periods
for t = 1:obj.NumPeriods
obj = obj.calculatePeriod(t);
end
end
end
end
To use this class, you would create an instance of “FinancialModel” and then call its methods to perform the calculations:
growthRate = 0.05;
contributionAmount = 100;
numPeriods = 10;
model = FinancialModel(growthRate, contributionAmount, numPeriods);
model = model.runModel();
% Now you can access the results
disp(model.Closing);
This modular approach allows you to encapsulate the logic for each calculation in its own method, which makes your code easier to read, maintain, and test. If you have different types of calculations or scenarios, you could extend this class or create different classes as needed.
For financial modelling specifically, MATLAB offers the Financial Toolbox, which provides functions for mathematical modelling and statistical analysis of financial data. This toolbox have pre-built functions that can simplify your modelling process.
I hope this helps!
Sameer
0 Comments
See Also
Categories
Find more on Data Import from 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!