The code provided is not recognizing the decimal_Fm as a variable, making this function uselless when placed into another m file. Any suggestions?

function percent_Fm = sarcomere_force(percent_L)
decimal_L = percent_L/100;
L = decimal_L*2.7; % micrometers
if 1.35<=L && L<=1.91
decimal_Fm = 1.41*L-1.91
elseif 1.91<L && L<=2.37
decimal_Fm = 0.40*L+0.04
elseif 2.37<L && L<=2.71
decimal_Fm = 0.04*L+0.89
elseif 2.37<L && L<=4.53
decimal_Fm=-0.50*L+2.35
end
percent_Fm = decimal_Fm*100;
As a note, I know there is no percent_L given. That is something solved for in the other m file.

Respuestas (1)

hello
I fixed your code :
  1. the function was missing and else statement in case the L values are not in the numerical range already coded. For this case you have to decide what value (or leave empty) you want.
  2. the function(s) are always located at the lower end of the script, you do the function call above the function script
clc
clearvars
%% main code
percent_L = 100;
percent_Fm = sarcomere_force(percent_L);
%% functions
function percent_Fm = sarcomere_force(percent_L)
decimal_L = percent_L/100;
L = decimal_L*2.7; % micrometers
if 1.35<=L && L<=1.91
decimal_Fm = 1.41*L-1.91;
elseif 1.91<L && L<=2.37
decimal_Fm = 0.40*L+0.04;
elseif 2.37<L && L<=2.71
decimal_Fm = 0.04*L+0.89;
elseif 2.37<L && L<=4.53
decimal_Fm=-0.50*L+2.35;
else % here
decimal_Fm=[]; % or ??? to be defined
end
percent_Fm = decimal_Fm*100;
end

3 comentarios

So, two things:
1) the percent_L isn't 100. Its a value calculated in the other m file
2) If I add the else -> decimal_Fm=[ ] into my code, it says it is an "illegal use of reserved keyword 'else'"
As a new update: I had the else portion after end, giving me the error message.
hello
for your function you need and additionnal "end" - that was missing in your code
  • there must be one "end" for the if loop
  • a second end to "close" the function
see my code example and double check yours
function percent_Fm = sarcomere_force(percent_L)
decimal_L = percent_L/100;
L = decimal_L*2.7; % micrometers
if 1.35<=L && L<=1.91
decimal_Fm = 1.41*L-1.91;
elseif 1.91<L && L<=2.37
decimal_Fm = 0.40*L+0.04;
elseif 2.37<L && L<=2.71
decimal_Fm = 0.04*L+0.89;
elseif 2.37<L && L<=4.53
decimal_Fm=-0.50*L+2.35;
else % here
decimal_Fm=[]; % or ??? to be defined
end
percent_Fm = decimal_Fm*100;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Mzo. de 2022

Editada:

el 3 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by