Interpolation values from a table

Hi guys, i wonder if anyone can help me! Im trying to intepolate values from a table that i load on to matlab but i dont understand what is the thing that i am doing wrong, can someone help me? Here is my code:
Screenshot 2019-09-09 at 17.55.35.png
Screenshot 2019-09-09 at 17.55.24.png
Screenshot 2019-09-09 at 17.55.08.png
I didnt put the whole code on here because i dont think it is necessary! Basically i got the areatemp1 file with has values for areas mach and temperatures and i want to interpolate to get a mach value after calculating a Area value. if anyone could help me, it would be great

댓글 수: 4

the cyclist
the cyclist 2019년 9월 9일
If you upload your actual code, instead of an image of your code, it is vastly easier for people to help you.
you are right, here it is:
tic
clear all
clc
file = 'areatemp1';
file1 = 'machtemp1';
opts = detectImportOptions(file,'NumHeaderLines',0);
opts1 = detectImportOptions(file1,'NumHeaderLines',0);
T = readtable(file,opts);
T.Properties.VariableNames = {'Area','Mach','Temp'};
t = readtable(file1,opts1);
t.Properties.VariableNames = {'mach','temp'};
%condições iniciais do problema
P0 = 18:25;
T0 = 25:50;
Tc = 50:121;
D0 = input ('What is the size of the hole in microns? ');
D0 = D0*10^-6;
A0 = ((pi*D0)^2)/4;
i = 0;
j = 0;
k = 0;
Patm = 1;
R = 8.314;
A01 = (1/5)*A0;
gama = 1.4;
for i = 1:length(P0);
P011(i) = 0.666*P0(i) + 0.333*Patm;
for j = 1:length(T0);
mass(i,j) = (0.6856*P011(i)*A01)/(R*T0(j))^(1/2);
rho0(i,j) = 0.9805 * (P011(i)/(R*T0(j)));
V0(i,j) = mass(j)/(rho0(j)*A0);
P01(i,j) = P0(i)-(rho0(j)*V0(j)^2);
T1(j) = T0(j) * 0.8316;
V1(j) = (gama*R*T1(j))^(1/2);
P1(j) = P01(i,j) * 0.5274;
for k = 1:length(Tc);
rhoe(k) = Patm/(R*Tc(k));
Areas(i,j,k) = 1/((((R*T0(j))^(1/2))*rhoe(k)*V1(j))/(1.3712*P01(i,j))+sqrt((((R*T0(j))^(1/2)*rhoe(k)*V1(j))/(1.3712*P01(i,j)))^2+(R*T0(j)*rhoe(k)*(P1(j)-Patm))/(0.6856*P01(i,j))^2));
%interpolaton
dif = Areas(i,j,k); %obtained however you want. Can be an array of temperatures
machs = interp1(T.Area, T.Mach, dif); %array of viscosities the same size as temperature
temps = interp1(T.Area, T.Temp, dif);
t1(j) = temps*T0(j);
temperatura = interp1(t.mach, t.temp, machs);
T2(j) = tempereratura*t1(j);
if T2(j)==Tc(k)
T2(j)=T2;
else
T2={};
end
end
end
end
toc
Dheeraj Singh
Dheeraj Singh 2019년 9월 12일
Please check the variable type of T.Area . You can print the variable to see if it is of expected type or not. You can refer to the documentation of interp1 to see the expected type of T.Area.
Hope this helps.
T.Area is a cell array of 31x1. Basically i have a table called 'areatemp1' which has values of area, mach number and temperatures. What im calculating is an area and i want to use that calculated area to interpolate on the table and export the values for mach number and temperature. I can do everything but once i get to the interpolation this error comes up:
Error using interp1>reshapeAndSortXandV (line 414)
X must be a vector of type double or single.
Error in interp1 (line 93)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in Tese (line 51)
machs = interp1(T.Area, T.Mach, dif); %array of viscosities the same size as temperature

댓글을 달려면 로그인하십시오.

답변 (1개)

Jon
Jon 2019년 9월 12일
편집: Jon 2019년 9월 12일

1 개 추천

If T.area is a cell array, and maybe T.Mach is also a cell array you must first turn them into ordinary MATLAB vectors, (n by 1 or 1 by n arrays) before interpolating. So, for example you could do something like
area = cell2mat(T.Area)
Mach = cell2mat(T.mach)
machs = interp1(area,mach,dif);
or perhaps more compactly
machs = interp1(cell2mat(T.area),cell2mat(T.mach),dif)

댓글 수: 6

Now it says wrong number of arguments. Thanks for all your help by the way.
Error using interp1>parseinputs (line 340)
Wrong number of input arguments.
Error in interp1 (line 78)
[method,extrapval,ndataarg,pp] = parseinputs(varargin{:});
Jon
Jon 2019년 9월 12일
What line in your code is the error being thrown from. Please copy and paste the whole error message, especially the part where it reprints the actual call to interp1
Sorry for the late reply, here it is:
Error using interp1>parseinputs (line 340)
Wrong number of input arguments.
Error in interp1 (line 78)
[method,extrapval,ndataarg,pp] = parseinputs(varargin{:});
Error in Tese (line 51)
machs = interp1(cell2mat(T.Area), cell2mat(T.Mach), dif); %array of viscosities the same size as
temperature
Jon
Jon 2019년 9월 16일
편집: Jon 2019년 9월 16일
It seems that somehow your arguments aren't really what you think they are.
interp1 expects three input arguments, and looking at the code superficially, it looks like you are supplying it with 3 input arguments, so it is not obvious what the error is. Somehow, one of the three arguments you are supplying must not be evaluating to just a vector. I would suggest stepping through the code with the debugger and checking to see what those arguments evaluate to just before the function is called. Put a breakpoint on the line that generates the error. See https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html and then look in the workspace window to see what T.Area, T.Mach, and dif really are at that point. If those are as expected, e.g. cell array 31 by 1 as for T.Area, then type on the command line cell2mat(T.Area) and see what that returns (should return a vector), repeat for cell2mat(T.Mach), and then just type dif to see what that gives. Hopefully you can figure out which of those arguments is somehow not a vector and fix it from there.
If you don't want to use the debugger (although it really is the best way to debug things) you can insert the lines to just before the call to interp1 that throws the error, as earlier suggested by @Dheeraj. Note no semicolon so output will print to you command window.
T.Area
T.Mach
diff
cell2mat(T.Area)
cell2mat(T.Mach)
Jon
Jon 2019년 9월 17일
Did this answer your question? In the end what was causing the wrong number of arguments error?
Andrea
Andrea 2024년 12월 5일
I can't speak for Tomas, but Jon your answer helped a lot! Thank you.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 9월 9일

댓글:

2024년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by