how to output inputname from varargin

Hi, I have a set of input arguments. I want to loop through one of them so i make the one (n) i want to loop a vector. Then I wrote a function file to test which one is the vector so matlab knows which one to loop. But I am having a problem bringing out the output as a variable from this function to then carry on with the loop.
rt = 3;
Ps = 5.3;
tt = 600;
Tjh = 1;
Tjc = 18;
n=[5,6,8,9];
tad=0.01328;
mc=0.001566;
ps=30;
dt=0.002;
A = 0.09;
Ay = 2;
Kad=4;
h=844;
function p= intest(varargin)
for i = 1:nargin
va(i)=(length(varargin{i}));
end
w=find(va>1);
p=inputname(w);
end
for....
Thanks.

Antworten (1)

Stephen23
Stephen23 am 22 Jan. 2016
Bearbeitet: Stephen23 am 22 Jan. 2016

0 Stimmen

With so many variables it would be a good idea to store them in a structure anyway, to avoid workspace clutter and to make passing and storing those variables much simpler. And when these variables are stored in a structure it also makes your task easy:
S.rt = 3;
S.Ps = 5.3;
S.tt = 600;
S.Tjh = 1;
S.Tjc = 18;
S.n=[5,6,8,9];
S.tad=0.01328;
S.mc=0.001566;
S.ps=30;
S.dt=0.002;
S.A = 0.09;
S.Ay = 2;
S.Kad=4;
S.h=844;
%
V = structfun(@numel,S);
C = fieldnames(S);
assert(nnz(V>1)==1,'Too many vectors')
for k = S.(C{V>1})
disp(k)
end
this detects that S.n is a vector, and prints its values in the loop:
5
6
8
9

4 Kommentare

Oluyemi Jegede
Oluyemi Jegede am 22 Jan. 2016
Thanks. But I have a body of code in the for loop where the variable n is used. Would this still work in that case?
Stephen23
Stephen23 am 22 Jan. 2016
Bearbeitet: Stephen23 am 22 Jan. 2016
If you want to loop over all of the scalar elements/values in S.n, but want to use the same fieldname inside the loop, then you can do this:
N = structfun(@numel,S); % number of elements
assert(nnz(N>1)==1,'Too many vectors')
C = fieldnames(S);
F = C{N>1}; % fieldname of non-scalar field.
V = S.(F); % data from that field
for k = V
S.(F) = k;
S.n % each scalar value of S.n
... code that uses all scalar values.
end
which in the loop prints each scalar value of S.n:
ans = 5
ans = 6
ans = 8
ans = 9
Inside the loop every variable will have to be replaced with the equivalent S.xxx.
Oluyemi Jegede
Oluyemi Jegede am 23 Jan. 2016
thanks!!!
Stephen23
Stephen23 am 23 Jan. 2016
My pleasure! You can also accept the Answer that best resolves your question.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 22 Jan. 2016

Kommentiert:

am 23 Jan. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by