フィルターのクリア

code works as part of main script but not as a function?

2 ビュー (過去 30 日間)
Laila Rezai
Laila Rezai 2021 年 7 月 15 日
コメント済み: Laila Rezai 2021 年 7 月 15 日
I want to make the parameter script of my simulation more orginsed und desided to some of its part as function. But the same code as function is not working.
the function, which I written is:
function y_fast=FAST_Power_Value(y_FAST)
if exist ('y_FAST.mat','file')
% function to load y_FAST.mat and create y_fast.mat with specified varaibles.
load('y_FAST.mat');
c = {'Time','GenPwr','GenTq','U','Upu','GenPwrpu'};
row = length(y_FAST{1,1}.OutData);
col = length(c);
j=1;
y_fast = struct([]);
for i = 1:length(y_FAST)
y_fast{i,1} = y_FAST{i,1};
for index = 1:length(y_FAST{i}.OutList)
if ~ismember(y_fast{i}.OutList(index),c)==1 % to replace cell's contain in the OutList with NaN if they are not member of c vector
y_fast{i,1}.OutList{index}='NaN';
end
if strcmp(y_fast{i,1}.OutList{index},'NaN')==0
y_fast{i,1}.Outdata(:,j) = y_fast{i,1}.OutData(:,index); % to copy the data of the nan cells in OutList to a new variable Outdata
j=j+1;
end
end
y_fast{i,1}.OutList = y_fast{i,1}.OutList(~strcmp(y_fast{i,1}.OutList,'NaN')); % To remove 'NaN' from OutList
y_fast{i,1}.OutData = y_fast{i,1}.Outdata;
j=1;
end
y_fast=cellfun(@(x)rmfield(x,'Outdata'),y_fast,'UniformOutput',0); % To remove Outdata field from structure
clear y_FAST row col c j index i;
end
end
and I called it in the main script using the command:
y_fast=FAST_Power_Value(y_FAST);
but gives the following errors
Unrecognized function or variable
'y_FAST'.
Error in para_sim_fl_par_com_update
(line 13)
y_fast=FAST_Power_Value(y_FAST);
althogh the y_FAST.mat exist in current folder.

採用された回答

KSSV
KSSV 2021 年 7 月 15 日
編集済み: KSSV 2021 年 7 月 15 日
y_FAST = 'myfile.mat' ; % give your file name here
y_fast=FAST_Power_Value(y_FAST) ;
And use this function:
function y_fast=FAST_Power_Value(y_FAST)
if exist (y_FAST,'file')
% function to load y_FAST.mat and create y_fast.mat with specified varaibles.
load(y_FAST);
c = {'Time','GenPwr','GenTq','U','Upu','GenPwrpu'};
row = length(y_FAST{1,1}.OutData);
col = length(c);
j=1;
y_fast = struct([]);
for i = 1:length(y_FAST)
y_fast{i,1} = y_FAST{i,1};
for index = 1:length(y_FAST{i}.OutList)
if ~ismember(y_fast{i}.OutList(index),c)==1 % to replace cell's contain in the OutList with NaN if they are not member of c vector
y_fast{i,1}.OutList{index}='NaN';
end
if strcmp(y_fast{i,1}.OutList{index},'NaN')==0
y_fast{i,1}.Outdata(:,j) = y_fast{i,1}.OutData(:,index); % to copy the data of the nan cells in OutList to a new variable Outdata
j=j+1;
end
end
y_fast{i,1}.OutList = y_fast{i,1}.OutList(~strcmp(y_fast{i,1}.OutList,'NaN')); % To remove 'NaN' from OutList
y_fast{i,1}.OutData = y_fast{i,1}.Outdata;
j=1;
end
y_fast=cellfun(@(x)rmfield(x,'Outdata'),y_fast,'UniformOutput',0); % To remove Outdata field from structure
clear y_FAST row col c j index i;
end
end
  1 件のコメント
Laila Rezai
Laila Rezai 2021 年 7 月 15 日
Thank you very much KSSV. It works now :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by