Hi, guys, I am trying to create a MATLAB Subrountine, but I faced some problems. My code is not working, and I don't know what is wrong with it.

1 回表示 (過去 30 日間)
Error:
("??? Error: File: EXPERI.m Line: 4 Column: 1
Function definitions are not permitted in this context.")
clc
clear
function [x] = getcond(t1, t2 , material);
% GETCOND calculates the conductance given the two temperatures and
% material involved.
%INPUTS:
% t1 - high temperature
% t2 - low temperature
% material - (string) material being used.
%OUTPUTS:
% returns the conductance
%EXAMPLE:
% result = GETCOND(300, 77, 'aluminum')
t1 = input('Enter Low Temperature');
t2 = input('Enter High Temperature');
material = input('Enter Constant, k');
%Classify according to "material"
if material = printf('aluminum_high');
k = [0.10974, 0.019819, -7.7701e-5, 1.04e-7]*100;
elseif material = printf('steel_high');
k = [-0.00551407, 0.001541448, -5.59e-6, 8e-9]*100;
else material = printf('Error: Do not recognize material');
k = [0];
end
cond = k[0]*(t1-t2)+k[1]*(t1^2-t2^2)/2.+k[2]*(t1^3-t2^3)/3.+k[3]*(t1^4-t2^4)/4;
x = cond / (t1 - t2);
end
T_1=100;
T_2=200;
K_1=getcond(T_1, T_2, 'aluminum_high')

回答 (2 件)

ES
ES 2013 年 9 月 4 日
Did you write your function in command window? Unlike python, MATLAB does not let you write a function in its command window. So create an m file with your function and call it from command line.
Thanks!
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 9 月 4 日
From the error message we can see that the code was put into EXPERI.m and not the command line.
Ilham Hardy
Ilham Hardy 2013 年 9 月 4 日
It "function inside the script problem", then?

サインインしてコメントする。


Laurent
Laurent 2013 年 9 月 4 日
Try to remove the first three lines ('clc', 'clear' and the empty line), then rename your file, or function, so that the filename and function name are the same. You also don't need the semi-colon behind the 'function' line.
After that I don't see why this should not work.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by