Assigning output from separate .m file to variable
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a script (that I didn't write) IdealAir.m, found here: https://www.mathworks.com/matlabcentral/fileexchange/25030-ideal-air-properties. I need to assign the outputs of IdealAir to variables multiple times within a function that I wrote.
IdealAir interpolates from a table. It takes 3 arguments (a value, the variable to which the value is assigned, and the output variable), and assigns the output to a variable created within the script. Possible input and output variables are T, h, u ,pr, vr, and so. Example: IdealAir(2.5,'so','h') h = 664.0107
A simplified version of what I'm trying to do:
function dualrankine(T4)
H4=IdealAir(T4,'T','h');
P4=IdealAir(H4,'h','pr');
disp(H4);
disp(P4);
end
Obviously this is wrong, it spits back the following:
Error using IdealAir
Too many output arguments.
Error in dualrankine (line 2)
H4=IdealAir(T4,'T','h');
I tried nesting IdealAir within dualrankine, but it gave the following:
Error: File: dualrankine.m Line: 115 Column: 4
The function "IdealAir" was closed with an 'end', but at least one other function definition was not. To avoid
confusion when using nested functions, it is illegal to use both conventions in the same file.
Disclaimer: I'm not very experienced with Matlab. I've looked around for solutions, but either I haven't found one or I haven't recognized it. Please just direct me to the solution if it exists.
0 commentaires
Réponse acceptée
Stephen23
le 26 Fév 2017
Modifié(e) : Stephen23
le 26 Fév 2017
"IdealAir interpolates from a table. It takes 3 arguments (a value, the variable to which the value is assigned, and the output variable)..."
Yes, it does.
"and assigns the output to a variable created within the script" ...
No, it does not. Don't worry, it is not your fault! The code certainly defines the variable, but this variable is not specified to be an output variable, so it never gets returned by the function!:
function IdealAir(xi,prop1,prop2,unit) % <- no output argument!
Whoever wrote that code (btw, it is a function and not a script) in their wisdom decided not to return any output arguments: it simply displays some values in the command window. This is rather bizarre, as expert code writers would consider that a function that generates useful values like this one does should simply return those values. The fact that the author named that final variable the same as the function name implies that the author has experience with VBA or some similar language, but sadly not with MATLAB (no h1 line is another big clue for that).
It is easy to change that function to return an output argument, simply change the function definition line to this:
function out = IdealAir(xi,prop1,prop2,unit)
and also these lines at the end of the file:
out = interp1(x,y,xi);
%
%displays on screen (optional step)
% disp([prop2,' = ',num2str(IdealAir)]);
When we make those changes the function returns an output argument, exactly as you require for your code:
>> IdealAir(2.5,'so','h')
ans =
664.01
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!