reading out variable input names where "inputname" gives an empty string

4 Ansichten (letzte 30 Tage)
Martin Stiehl
Martin Stiehl am 25 Mär. 2020
Bearbeitet: Stephen23 am 25 Mär. 2020
Hi everyone,
i am writing an class with properties and methods to analyse my data
classdef Data < matlab.mixin.Copyable
properties
data1 % arrays 500x4 double
data2 % arrays 500x4 double
data3 % arrays 500x4 double
value
end
methods
function add_data (obj,x,y)
for m = 1:nargin
disp(['Calling variable ' num2str(m) ' is ''' inputname(m) '''.'])
end
obj.value = x + y;
end
end
end
%trying to track which inputs were used in the function
test=Data;
test.add_data(test.data1,test.data3);
--> Result:
Calling variable 1 is 'test'.
Calling variable 2 is ''.
Calling variable 3 is ''.
and now trying to add an log to it, that records all methodes used to be able to later on reproduce the results
the problem i ran into is that using inputname() i dont get out which data was used for x and y in the function, it just returns an empty string
anyone got an idea how to get the names, so in this case
--> Result i would like to get:
Calling variable 1 is 'test'.
Calling variable 2 is 'data1'.
Calling variable 3 is 'data3'.
  6 Kommentare
Stephen23
Stephen23 am 25 Mär. 2020
Bearbeitet: Stephen23 am 25 Mär. 2020
"but they are indexing from the propertys which are variables or?"
subsref indexing accesses a part of an array and creates a new array in memory. That new array can certainly be allocated to a variable (which has a name) but often they are used directly without being allocated to a variable (e.g. the input arguments in your example code): the origin of that new array is NOT stored as one of its properties (and there is no way to go back up the entire chain of indexing operations to know any possible variable that the new array might have come from).
"so shouldnt there be a way to get the name of the variable they are indexing from?"
Short of parsing the code yourself, there is no documented way to get all of the names of variables that are used in an expression.
Martin Stiehl
Martin Stiehl am 25 Mär. 2020
then it cant be helped thx anyway for the help

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by