Mixing name/value pair syntax
103 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
In a Matlab seminar a few years ago, I asked why function calls that mix old style and new style name/value pair syntax would only work in a certain order. Specifically, the old style pairs must come first in the argument list. When the order is reversed, an error is thrown as illustrated below.
testFunc('A',10, B=20)
testFunc(B=20, 'A',10)
A MathWorker at the seminar told me that this was by design, but there was no time for him to elaborate. Can anyone think why this would have been a deliberate design choice?
function testFunc(opts)
arguments
opts.A=1;
opts.B=2;
end
disp(opts)
end
12 Kommentare
Rik
am 9 Jan. 2026 um 17:07
Isn't there a single parser that looks for syntax errors? Why would the naive approach of making the Name=Value syntax work the same as {:} cause any issues anywhere? And why there be a requirement for Name=Value to be last either way?
Only partially on topic: I think the monstrosity below shows how poorly I understand what is happening.
tryme(A=10)
function tryme(A,B)
arguments
A=1;
B=2;
end
A
B
end
Antworten (0)
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!