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

2 vues (au cours des 30 derniers jours)
Laila Rezai
Laila Rezai le 15 Juil 2021
Commenté : Laila Rezai le 15 Juil 2021
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.

Réponse acceptée

KSSV
KSSV le 15 Juil 2021
Modifié(e) : KSSV le 15 Juil 2021
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 commentaire
Laila Rezai
Laila Rezai le 15 Juil 2021
Thank you very much KSSV. It works now :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by