1

Assume I have the function:

function name_the_paramlist(varargin)
    % Print out varargin exactly how it is called as a string

Basically what I want it to do is, calling:

name_the_paramlist({'x', x}, y, 1, 'hello', [1, 2; 3, 4])

should print to the screen the string:

'{''x'', x}, y, 1, ''hello'', [1, 2; 3, 4]}'

Any suggestion?

ETA: The reason I want something like this is to hopefully resolve this question: Matlab - how to create a subclass of dataset class keeping the dataset parameter constructor

Community
  • 1
  • 1
cinny
  • 2,292
  • 3
  • 18
  • 23
  • I don't think it's possible. Once you pass a variable like `x` or `y` to a function, it does not know about the variable name, only values. `varargin` is a cell array of input arguments, not a string. I'm curious what do you need it for? – yuk Mar 22 '12 at 06:18
  • I didn't see this comment earlier but I answered it below your answer. – cinny Mar 23 '12 at 16:04

1 Answers1

3

Well, if you type this function call in a command line or run with F9 (so it get saved in the history) you can read the history.m file and get the last command as a string.

fid = fopen(fullfile(prefdir,'history.m'),'rt');
while ~feof(fid)
    lastcmd = fgetl(fid);
end
fclose(fid);

Then get the argument part:

arg_str = regexp(lastcmd, '\w+\((.+)\)','tokens','once');
arg_str = strrep(arg_str{:},'''','''''');

UPDATE:

Another idea. If you call this function from another script or function (m-file) you can use DBSTACK to return the m-file name and the current line number:

SI = dbstack;
filename = SI(end).file;
lineNo = SI(end).line;

Then you can follow similar technique as in the first solution to read that line from the m-file and get argument part.

In opposite to the first solution this won't work if run from command line or editor cell mode.

yuk
  • 19,098
  • 13
  • 68
  • 99
  • This is good to know. Though I need this to work in all cases, i.e. when the function is called from another function or script for example. – cinny Mar 22 '12 at 16:00
  • Thanks Yuk. Is there a way to distinguish between the 2 situations inside the code? Also, just to clarify why I want smth kind of strange like this: It's because it may be a way to resolve my question over here where no one had an answer yet: http://stackoverflow.com/questions/9810809/matlab-how-to-create-a-subclass-of-dataset-class-keeping-the-dataset-parameter. Please let me know if you have a direct answer to that question. – cinny Mar 23 '12 at 16:01
  • You can check if `SI.file` have entries other then function called `dbstack`. Anyway I think it's a weird way to solve your problem. Will have a look at the previous question, but I don't have much experience with oop. Consider giving a bounty, that really attracts many people. – yuk Mar 23 '12 at 16:24
  • The other problem does not have much to do with OOP actually. Dataset() is a class, so in that way it's OOP. But the issue lies with preserving the workspace variables names when things are passed down. – cinny Mar 23 '12 at 16:28
  • How about if you pass existing dataset variable to subclass constructor as an argument? It can be made optional. Sorry, if I don't understand the problem yet. – yuk Mar 23 '12 at 16:32
  • So mydataset() is a subclass of dataset(). A subclass usually usually uses methods already given by the class, and then modify / add to it. In this case, I want mydataset() to firstly do what dataset() does, and then I do my modifications later. Does that make sense? – cinny Mar 24 '12 at 05:18