Reading data from griddata - issue with type of variable.

When I tried to read data from a griddata, I got following error." Attempted to access SourcePV1(0.5,0.5); index must be a positive integer or logical"; please let me know what should I do now?

4 Comments

Well it looks like the index is not an integer. When you have a=[1 2], then you can't expect a value for a(1.2), can't you? To solve the root cause of the problem, please psot more details.
the script does look like this...
----------------------------------------
Zi = 0.01:0.01:1;
PVi = 0.01:0.01:1;
z = 0.600;
pv = 0.600;
[qx,qy] = meshgrid(Zi,PVi);
SourcePV1 = griddata(Z1,PV1,sPV1,qx,qy);
Enthalpy1 = griddata(Z1,PV1,H1,qx,qy);
spv1 = SourcePV1(z,pv);
------------------------------------------
here Z1,PV1,sPV1 and H1 are the vectors.
where, [qx,qy] is a grid with decimel indices..so I should find a value - SorucePV1(0.5,0.5)..is nt it?
No. Zi is defined here, not Z1. If that is your script, perhaps you might tell us what is Z1? For that matter, what is sPV1? How about PV1, also undefined, while you have given us PVi. Check your data. Consider there is a difference between i and 1. I am sure MATLAB knows the difference.
Here is the Script for more comprehensive idea on what I am intending to do...
---------------------------------------------------------
clear all
clc
format long e
% Reading the data from laminar table Spv vs PV vs Z
C = [];D=[];E=[];
% double z;
% double pv;
R1 = importdata('FGM_ToxHigh_1340K_353K_152bar_nuspicset_enthalpy.dat','\t',11);
Z1 = R1.data(:,1);
PV1 = R1.data(:,2);
sPV1 = R1.data(:,16);
H1 = R1.data(:,17);
% matrix of Z,PV for which sPV will be interpolated at R2
Zi = 0.01:0.01:1;
PVi = 0.01:0.01:1;
z = 0.5;
pv = 0.5;
[qx,qy] = meshgrid(Zi,PVi);
SourcePV1 = griddata(Z1,PV1,sPV1,qx,qy);
Enthalpy1 = griddata(Z1,PV1,H1,qx,qy);
spv1 = SourcePV1(z,pv);
h1 = Enthalpy1(z,pv);
------------------------------------------------------------

Sign in to comment.

Answers (1)

The reason why you receive this error is that you try to access SourcePV1(0.6,0.6) where SourcePV1 is a matrix. There is no concept in MATLAB of non-integer indexes of matrices and also not <1.
What you probably want can be achieved by interpolating between the values SourcePV1(1,1), SourcePV1(1,2), SourcePV1(2,1) and SourcePV1(2,2).

2 Comments

but [qx,qy] is a grid with Zi and PVi indices which are non-integers?. So how could I read a value from matrix SourcePV1 for z=0.5 and pv=0.5? should I assign a counter m,n for z=0.5 and pv=0.5 from [qx,qy] and look value for that counter m,n in SourcePV1??
You should probably use interp2() with 'nearest' option.
Your Zi and PVi vectors do not contain the values that you think they do. For example, try
sprintf('%.90g\n', Zi(1))
Notice that it is not _exactly_ 0.01. The next element will not be _exactly_ 0.02 and so on. When you construct a vector using an increment such as 0.01 then eventually you will get an element that _looks_ like what you think it should be, but which does not have exactly the same internal value as converting the decimal value would. For example,
>> Zi(7) - 0.07
ans =
-1.38777878078145e-17
Because of this, if you did attempt to find 0.07 in your vector, you would not find it, interp2() should, though, be able to easily determine the nearest point to 0.07 .
Please read http://matlab.wikia.com/index.php?title=FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!