fprint error

17 views (last 30 days)
joo tan
joo tan on 21 Jun 2012
Itry process multiple data..sometimes, my programming working..but,when i want to repeat processing, appear error like below..can someone help me to solve this error
??? Error using ==> fprintf Function is not defined for 'struct' inputs.
Error in ==> Dailystep2 at 154 fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',LAT,LON,ug,vg,dir,v,EKE,he1);

Answers (4)

Image Analyst
Image Analyst on 21 Jun 2012
You need to specify particular fields in your struct, like ug.field1, vg.field42, v.myField, or whatever...

Geoff
Geoff on 21 Jun 2012
Like the error message says, some of your inputs are structs, and that's not allowed. Run the following code to check the types that you are passing to fprintf:
cellfun( @class, {LAT,LON,ug,vg,dir,v,EKE,he1}, 'uni', 0 )
  3 Comments
joo tan
joo tan on 21 Jun 2012
i think, it has problem with "dir"..because some data, the value is "NaN"..
if(ug>0)& (vg<0);
dir=90+(atand(abs(vg)/abs(ug)));
else
if (ug<0)& (vg>0);
dir=270+(atand(abs(vg)/abs(ug)));
else
if (ug<0)& (vg<0);
dir=270-(atand(abs(vg)/abs(ug)));
else
if (ug>0)&(vg>0);
dir=90-(atand(abs(vg)/abs(ug)));
else
if (isnan(hd1)) || (isnan(hd2));
dir=NaN;
end
end
end
end
end
Geoff
Geoff on 21 Jun 2012
Like walter said, don't use 'dir'. The problem here I think is that you do not guarantee that your variable will have a value. When you used 'dir', it wasn't so obvious because it just called the function instead of using the overridden variable name. You need something to fall back on. It seems like you use NaN for this purpose, but if ug and vg are both zero, and neither hd1 nor hd2 are NaN, you currently do not give 'd' any value.

Sign in to comment.


joo tan
joo tan on 21 Jun 2012
this is my programming..i am very dispointed when my programming not working like before..plz..help me
cd('E:\Data2\Gridfile');
files = dir('*.TXT');
for i=1:numel(files);
%for i=1:10
% option n = [files(i).name '-ascii']
n2 = [files(i).name];
fid=fopen(n2) ; % the original file
f = load(files(i).name);
P=n2;
N='FILE1.txt';
M='FILE2.txt';
M1='FILE3.txt';
R=6371000;
pi=7.27*(10^-5);
g=9.8;
for ii=226:225:16651 ;
for j=450:225:16875;
Lat=f(ii:j,1);
Lon=f(ii:j,2);
SLA1=f(ii:j,3);
end
for l=1:223;
LON1=Lon(l,1);
LON2=Lon(l+2,1);
h1=SLA1(l,1);
h2=SLA1(l+2,1);
he=SLA1(l+1,1);
fid=fopen(N,'a');
fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',LON1,LON2,h1,h2,he);
fclose(fid);
end
end
for k=1:225:16426;
for n=225:225:16650;
Lat=f(k:n,1);
SLA2=f(k:n,3);
end
for l=1:223;
LAT1=Lat(l+1,1);
h3=SLA2(l+1,1);
fid=fopen(M,'a');
fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',LAT1,h3);
fclose(fid);
end
end
for k1=451:225:16876;
for n1=675:225:17100;
Lat=f(k1:n1,1);
SLA3=f(k1:n1,3);
end
for l=1:223;
LAT2=Lat(l+1,1);
h4=SLA3(l+1,1);
fid=fopen(M1,'a');
fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',LAT2,h4);
fclose(fid);
end
end
F1 = load(N);
F2 = load(M);
F3 = load(M1);
for I=1:16502;
Stn=(I);
format LONG
lat1=F2(Stn,1);
lat2=F3(Stn,1);
lon1=F1(Stn,1);
lon2=F1(Stn,2);
he1=F1(Stn,5);
h11=F1(Stn,3);
h22=F1(Stn,4);
h33=F2(Stn,2);
h44=F3(Stn,2);
if h11~=NaN && h22~=NaN;
h12=h22-h11;
else
h12=NaN;
end
if h44~=NaN && h33~=NaN;
h13=h44-h33 ;
else
h13=NaN;
end
if isnan(h12);
h13=NaN;
end
if isnan(h13);
h12=NaN;
end
hd1=h12;
hd2=h13;
x1=R*(deg2rad(lon1))*cos(deg2rad(lat1));
x2=R*(deg2rad(lon2))*cos(deg2rad(lat2));
y1=R*(deg2rad(lat1));
y2=R*(deg2rad(lat2));
deltx=x2-x1;
delty=y2-y1;
LAT=(lat1+lat2)/2;
LON=(lon1+lon2)/2;
f = 2*(7.292115*(10^-5))* cos(deg2rad(lat1));
ug = (-1)*(g/f)*(hd2/delty) ;
vg = (g / f )*(hd1/deltx) ;
v=sqrt(ug^2+vg^2);
EKE=(ug^2+vg^2)/2;
% Derive geostrophic current velocity and direction
if(ug>0)& (vg<0);
dir=90+(atand(abs(vg)/abs(ug)));
else
if (ug<0)& (vg>0);
dir=270+(atand(abs(vg)/abs(ug)));
else
if (ug<0)& (vg<0);
dir=270-(atand(abs(vg)/abs(ug)));
else
if (ug>0)&(vg>0);
dir=90-(atand(abs(vg)/abs(ug)));
else
if (isnan(hd1)) || (isnan(hd2));
dir=NaN;
end
end
end
end
end
cd('E:\Data2\Gridfile\Result');
fid=fopen(P,'a');
%fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',lat1,lon1,ug,vg,dir,v);
fprintf(fid,'\n%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n',LAT,LON,ug,vg,dir,v,EKE,he1);
fclose(fid);
end
cd('E:\Data2\Gridfile');
delete(N);
delete(M);
delete(M1);
end

Walter Roberson
Walter Roberson on 21 Jun 2012
Do not use "dir" as a variable name, as it conflicts with the function name "dir".
There are circumstances under which your if/else structure might not set the variable named "dir", so when your code tries to reference the unset variable "dir" it instead gets the function named "dir", runs that function which returns a structure...
"dir" as a variable can end up not being set if vg or ug come out as exactly 0, because you use < 0 and > 0 and do not consider exactly equal to 0.
  4 Comments
Walter Roberson
Walter Roberson on 21 Jun 2012
Using "elseif" would help prevent all those hanging "end" statements that give the impression to the reader that they should be looking back for a matching "for" loop.
Geoff
Geoff on 21 Jun 2012
joo, check the comment in my answer. i suspect you never set a value for 'dir' (or 'd') because your block of if-statements does not handle one specific case, and you don't set the value anywhere else.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!