How to set input in a dynamic way, by means of indirect references to an object or other variable
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello to everybody,
I created a curve object "CurveName" with IRDataCurve and then I derived a rate structure from it by using toRateSpec function. Since I have several curves to choose among depending on the type of instrument to calculate the present value for, I need to set the name of the curve in a dynamic way in order to assign different curves to the DiscFactorsCurveName variable and maintain constant the script.
CurveName is the curve object (discount type)
Dates is the vector of input dates
DiscFactorsCurveName=toRateSpec(CurveName,Dates);
I need to assign a different curve object to the CurveName, for example Curve1, Curve2, Curve3, etc...
Have you any suggestions please?
Thank you very much in advance.
Angelo
5 Kommentare
Steven Lord
am 19 Mär. 2024
maintain constant the script.
If you're writing this code as a script file, I'd consider converting it into a function file. That way, whatever name you use inside the function will be completely independent of what name(s) you use outside.
% part 1 of example
x1 = [1 2 3 4];
p = myprod(x1)
x2 = 5:8;
p = myprod(x2)
x3 = [9 10; 11 12];
p = myprod(x3)
% Part 2 of example
X = randperm(12)
for k = 1:4:12
fprintf("Taking the product of X(%d:%d) = %s\n", k, k+3, mat2str(X(k:k+3)))
p = myprod(X(k:k+3))
end
function theOutput = myprod(theInput)
theOutput = prod(theInput(:));
end
Note that I did use numbered variable names x1, x2, and x3 in the first part of the example. I did that for illustration purposes only. When I wanted to "automate" this, in the second, I created "unnamed" temporary variables using indexing to pass into myprod. Note that nowhere outside myprod did I create any variables named theInput or theOutput, but MATLAB was able to handle passing the data into and out from the function just fine.
Antworten (1)
Shivam
am 17 Mär. 2024
Hello Angelo,
Based on your shared information, it seems you are interested in dynamically allocating various IRDataCurve objects and their corresponding RateSpec objects to distinct variables.
A systematic way to handle this would be to use MATLAB's structures to store and retrieve these objects. This method lets you dynamically assign names and efficiently organize multiple curves with their respective RateSpec objects.
You can have a look at the example code that demonstrates how IRDataCurve and RateSpec objects are stored in structures under dynamically generated names:
curve = struct();
rateSpec = struct();
for i = 1:2
curveName = sprintf('Curve%d', i); % Curve will be created as Curve1, Curve2, Curve2, etc.
% Creating dummy data
CurveSettle = datetime(2016,3,2);
Data = [2.09 2.47 2.71 3.12 3.43 3.85 4.57 4.58]/100;
Dates = datemnth(CurveSettle,12*[1 2 3 5 7 10 20 30]);
% Create IRDataCurve object dynamically
curve.(curveName) = IRDataCurve('Zero',CurveSettle,Dates,Data,'Compounding',4,'Basis',4);
% Create rateSpec object dynamically
rateSpecName = sprintf('RateSpec%d', i);
rateSpec.(rateSpecName) = toRateSpec(curve.(curveName), Dates);
end
Also, you can access a specific object as follows:
selectedCurve = curve.Curve2;
selectedRateSpec = rateSpec.RateSpec2;
You can refer to the documentation provided to know more about 'IRDataCurve' and 'toRateSpec':
- https://www.mathworks.com/help/releases/R2024a/fininst/irdatacurve.html
- https://www.mathworks.com/help/releases/R2024a/fininst/toratespecirdatacurve.html
I hope it helps.
3 Kommentare
Shivam
am 19 Mär. 2024
Hi Stephen,
In the case where you provide a specific curve name rather than using the generic Curve1, Curve2, and so forth, the described approach remains effective. Rather than dynamically generating the name, you have the option to store the curve objects under the names you provide. If this doesn't solve the problem, please do let me know.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!