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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by