This is not my code, it is used from an answered question on here. How could I functionalize it?
조회 수: 3 (최근 30일)
이전 댓글 표시
[num,txt,raw] = xlsread('p.xls') ;
P = num(:,1) ;
RV = num(:,2) ;
D = num(:,3) ;
% Pick a pressure value
Pi = input('Input pressure value:','s') ;
Pi = str2double(Pi) ;
tol = 10^-3 ;
idx = abs(P-Pi)<=tol ; % check is Pi present in data
if any(idx)
fprintf('%f is present in the data\n',Pi) ;
Pi = P(idx) ;
RVi = RV(idx) ;
Di = D(idx) ;
else
fprintf('%f is not present in the data, interpolating\n',Pi) ;
Rvi = interp1(P,RV,Pi) ;
Di = interp1(P,D,Pi) ;
iwant = [Pi RVi Di] ;
end
fprintf('Pressure = %f, R.V = %f, Liq.Density = %f\n',Pi,RVi,Di) ;
댓글 수: 0
채택된 답변
Image Analyst
2022년 4월 27일
What do you want to be an input argument? The filename? The Pi value? Any other values? Something like this maybe
function iwant = ComputeValues(filename, Pi)
iwant = []; % Initialize
[num,txt,raw] = xlsread(filename) ;
P = num(:,1) ;
RV = num(:,2) ;
D = num(:,3) ;
% Pick a pressure value
Pi = str2double(Pi) ;
tol = 10^-3 ;
idx = abs(P-Pi)<=tol ; % check is Pi present in data
if any(idx)
fprintf('%f is present in the data\n',Pi) ;
Pi = P(idx) ;
RVi = RV(idx) ;
Di = D(idx) ;
else
fprintf('%f is not present in the data, interpolating\n',Pi) ;
Rvi = interp1(P,RV,Pi) ;
Di = interp1(P,D,Pi) ;
end
iwant = [Pi, RVi, Di];
fprintf('Pressure = %f, R.V = %f, Liq.Density = %f\n', Pi, RVi, Di) ;
댓글 수: 2
Image Analyst
2022년 4월 28일
You can certainly use the input() function wherever you want to get a value for Pi or any other variable. You can use it both in your main program before you call the function to get input argument values that you are going to pass. You can also use it in the function if you need to get additional values.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!