code works as part of main script but not as a function?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!