passing strings to a function to call commands

1 回表示 (過去 30 日間)
Rashmil Dahanayake
Rashmil Dahanayake 2014 年 5 月 22 日
編集済み: Rashmil Dahanayake 2014 年 5 月 22 日
I'd like to create a function to automate the passing variable names to statement. I tried to create a function to achieve it using is varargin and foo loop. The challenge is to pass unknown number for string names to a function call.
Manual method is as follow.
Vdc(1,:) = logsout.get('Vdc').Values.Data(:,1) ;
Vc(1,:) =logsout.get('Vc').Values.Data(:,1) ;
I_L(1,:) =logsout.get('I_L').Values.Data(:,1) ;
The function I created with errors
function logger(varargin)
% input is in the format logger(Vdc,Vin,Iload)
% where "logsout" is the name of the object stored Vdc,Vin,Iload,IL,,,,
% I want to automatically extract selected variables and store them with
% the original name
nsize=nargin;
for k=1:size
tag=spprintf(varagin(k));
data(:,1) = logsout.get('Vdc').Values.Data(:,1) ; % where tag='Vdc'
assignin('base',tag,data);
end
Any suggestions appreciated.
  1 件のコメント
Yao Li
Yao Li 2014 年 5 月 22 日
編集済み: Yao Li 2014 年 5 月 22 日
I had a quick look at your codes. At least, size is undefined and spprintf should be sprintf. In addition, varargin should be a cell array, and there is a typo in varagin(k). It should be changed to varargin{k}

サインインしてコメントする。

回答 (1 件)

Rashmil Dahanayake
Rashmil Dahanayake 2014 年 5 月 22 日
編集済み: Rashmil Dahanayake 2014 年 5 月 22 日
I figured it out for a certain extent.
function logdata2=(A)
% logvar={'Vdc','I_L'}; % pass this to the function
for z=1:length(logvar)
xxz(1,:) = logsout.get(logvar{z}).Values.Data(:,1) ;
assignin('base',logvar{z},xxz);
end
Now the challenge is instead of using intermediate variable,which contains string characters (ie ' symbol), is it possible to call the function as loadata(Vdc,IL);

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by