pass arguments to a function that have not been defined in the workspace
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
Hi all,
What am I supposed to do in order to achieve the following:
function op=Mset(op,ip,kk,n)
if kk==1
op=ip./n;
else
op = op + (ip./n);
end
Simply, I would like to pass 'op' which is an undefined variable to the function. In other words, op merely represents a bunch of characters or the name of the variable.
3 Kommentare
Adam
am 15 Jun. 2018
You need to pass an actual variable, not the name of a variable, which is only relevant within the context of its own workspace.
Given the code, op has to exist if kk is not equal to 1. If kk is equal to 1 then you could pass in [] as the first argument (or indeed literally anything if you really want!) as it will not be used.
Stephen23
am 15 Jun. 2018
"In other words, op merely represents ... the name of the variable."
Don't do this. Dynamically accessing variables names is how beginners force themselves into writing slow, complex, buggy code. Read this to know why:
It is much more efficient to use indexing, fieldnames, or the columns/rows of a table.
Aneesh Vasudev
am 19 Jun. 2018
Antworten (0)
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!