Question

I am creating a function with two inputs, and would like to know how I can name the output accorinding to the name of the inputs.

E.g.

function  [padded_*input_name*] = padstruct(S, F)

later in code...

 else
    padded_*input_name* = padarray(toPad, (longest - size(toPad,1)), NaN('double'), 'post');

So if I enter a struct (labelled as S in the input arguments) called my_struct, I would like to get an output called padded_my_struct. But this should be free to change according to the name of the input struct. I want to do this because I have a lot of structs to run through this function and I want to explicitly know from the output name, which is which.

I am a beginner with Matlab and so would appreciate any exaplanation or references to literature.

Was it helpful?

Solution

I don't quite give you what you want. Instead, I have a function that, given the inputs Account and F, produces in the base workspace a variable "padded_Account", and returns the name of the variable it creates.

function padded_Sname_str=padstruct(Sin,F)

%get the name of the structure used in the function call here
Sname=inputname(1);

%do stuff to Sin here
Sin.pad=F;  %this is just my test operation

%create the name of the new variable you want
padded_Sname_str=['padded_',Sname];

%this creates it in the base workspace, since passing back as an
%output argument doesn't preserve the name
assignin('base',padded_Sname_str,Sin)

return

Hope this is helpful. Cheers!

OTHER TIPS

Try this -

function  [padded_*input_name*] = padstruct(S, F)

%// .. later on in code
else
    value = padarray(toPad, (longest - size(toPad,1)), NaN('double'), 'post');
    evalc(['padded_' inputname(1) '= value']);

Let us know if it works for you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top