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!