Getting error that output argument is not assigned during function when it is?

6 views (last 30 days)
I have a function that finds wind speeds between certain lats and lons. This is the first line in my function:
function [d,ccmp]=CCMPLidar2018(latmin,latmax,lonmin,lonmax)
This is my code for when I call the function:
[d,ccmp]=CCMPLidar2018(latmin,latmax,lonmin,lonmax);
It looks exactly alike right? However, I'm getting this error:
[d,ccmp]=CCMPLidar2018(latmin,latmax,lonmin,lonmax);
Output argument "ccmp" (and maybe others) not assigned during call to "CCMPLidar2018".
I just have no idea why I'm getting this error. The function and script are in the same folder, the argument ccmp is included in the function. Does anybody have any idea why I'm getting the error?
  2 Comments
Stephen23
Stephen23 on 24 Nov 2021
Edited: Stephen23 on 24 Nov 2021
"Does anybody have any idea why I'm getting the error?"
Yes: because the output is not defined inside the function. Just like the error message states.
"Getting error that output argument is not assigned during function when it is?"
Because it isn't.
This depends on the code in your function, not on the function signature (which you showed us).
Usually beginners get this error when they have some IF/ELSEIF/ELSE logic or SWITCH logic or something similar which does not define the output variables on all possible paths. But as you did not show any of the relevant code, we cannot debug this.
If you want more help, you will have to show us your function code.
Shayma Al Ali
Shayma Al Ali on 24 Nov 2021
Here is the function code:
function [d,ccmp]=CCMPLidar2018(latmin,latmax,lonmin,lonmax)
%cd('G:\Shared drives\Saltzman Aydin lab data\Shayma\LIDAR\CCMP\2018')
d.lat=[]; d.lon=[]; d.wspd=[];d.time=[];d.dirname=[];d.fname=[]; d.uwind=[];
d.vwind=[];
direc=dir('*.nc');
for j=1:length(direc) %get data from each file
%disp(['working on...' scat_dir(j).name]);
fname={direc(j).name}; %file name as cell array
ccmp_lon=ncread(direc(j).name,'longitude');
ccmp_lat=ncread(direc(j).name,'latitude');
ccmp_time=ncread(direc(j).name,'time');
ccmp_uwind=ncread(direc(j).name,'uwnd');
ccmp_vwind=ncread(direc(j).name,'vwnd');
%get datenum and datetime
d.datenum=[];
for i=1:length(ccmp_time)
d.datenum(i,:)=addtodate(datenum(1987,1,1),fix(ccmp_time(i,:)),'hour');
end
d.datenum=d.datenum';
dt=datetime(d.datenum,'ConvertFrom','datenum');
%%get the data from two time dimensions and then average
%average two vectors from uwind and vwnd
uwnd1=squeeze(ccmp_uwind(:,:,1));
uwnd2=squeeze(ccmp_uwind(:,:,2));
uwnd3=squeeze(ccmp_uwind(:,:,3));
uwnd4=squeeze(ccmp_uwind(:,:,4));
vwnd1=squeeze(ccmp_vwind(:,:,1));
vwnd2=squeeze(ccmp_vwind(:,:,2));
vwnd3=squeeze(ccmp_vwind(:,:,3));
vwnd4=squeeze(ccmp_vwind(:,:,4));
wind1=sqrt(uwnd1.^2+vwnd1.^2); %calculate wind speed for first one
wind2=sqrt(uwnd2.^2+vwnd2.^2);
wind3=sqrt(uwnd3.^2+vwnd3.^2);
wind4=sqrt(uwnd4.^2+vwnd4.^2);
%calculate wind speedsfor every point
%wspd=(wind1+wind2+wind3+wind4)/4;
%find if scat lon and lat are valid and where
v_lat=ccmp_lat<=latmax & ccmp_lat >=latmin;
v_lat=find(v_lat==1); ccmp.lat=ccmp_lat(v_lat);
v_lon=ccmp_lon<=lonmax & ccmp_lon >=lonmin;
v_lon=find(v_lon==1);
ccmp.lon=ccmp_lon(v_lon);
%extract the wind speeds for specific region
%wspd dimensions is lonxlat so flip
wind1=wind1(v_lon,v_lat);
wind2=wind2(v_lon,v_lat);
wind3=wind3(v_lon,v_lat);
wind4=wind4(v_lon,v_lat);
%create 3 dimensional matrix of lat lon with time
%so (lat x lon x time) dimensions
ccmp.wind1(:,:,j)=wind1;
ccmp.wind2(:,:,j)=wind2;
ccmp.wind3(:,:,j)=wind3;
ccmp.wind4(:,:,j)=wind4;
%transpose the winds
% wind1=pagetranspose(wind1);
% wind2=pagetranspose(wind2);
% wind3=pagetranspose(wind3);
% wind4=pagetranspose(wind4);
%Get windspeeds for valid lat and lon
d.lat=ccmp.lat;
d.lon=ccmp.lon;
d.time=[d.time;dt];
save('ccpwind_2018.mat','d')
end
I guess its important to note that I've use this function various times before and just recently had this problem.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 24 Nov 2021
Edited: Stephen23 on 25 Nov 2021
When direc is empty then ccmp is not defined (exactly as the error message states).
This error is easy to demonstrate (showing that the problem is in your code, not the function signature):
[A,B] = test([])
Output argument "y" (and possibly others) not assigned a value in the execution with "solution>test" function.
function [x,y] = test(z)
x = 1;
for k = 1:numel(z)
y = 2;
end
end
The solution depends on what you want to do when no files are found: you could throw an error, search for some other files, or define a default value before the loop, e.g.:
ccmp = struct();
for j = ...
end
Note that the code overwrite some of ccmp's fields on each loop iteration.

Community Treasure Hunt

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

Start Hunting!