This is not my code, it is used from an answered question on here. How could I functionalize it?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
ML
el 27 de Abr. de 2022
Comentada: Image Analyst
el 28 de Abr. de 2022
[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 comentarios
Respuesta aceptada
Image Analyst
el 27 de Abr. de 2022
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 comentarios
Image Analyst
el 28 de Abr. de 2022
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre String Parsing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!