Passing values from an array to a function

7 ビュー (過去 30 日間)
Thomas Bainbridge
Thomas Bainbridge 2022 年 8 月 2 日
回答済み: Varun 2023 年 9 月 1 日
I am trying to use functions to simplify a model. As part of this work I have used a function to calculate a value which is then output in the main code and saved to an array. From there a second function is required to take the value just entered into the matrix and use it to calculate a further value.
for i=1:n
Pressure_vessel(1,i) = VesselEvacuation(P_intial, S, LR, radius, height, Vol_sample, time_plot) * 100; % *100 to convert to Pa
vessel(1,i) = Pressure_vessel(1,i);
mass_plot(1,i) = CCVP(vessel(1,i), delHvap, R, T1, T2, Vol_sample, RMM);
t(1,i+1) = time_plot + 1;
time_plot = time_plot + 1;
mass_plot(1,i+1) = 0;
Upon trying this however I get the error:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Does anyone know a solution?
edit: swapped n for i
  5 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 8 月 3 日
It will be better if you post your whole code.


回答 (1 件)

Varun 2023 年 9 月 1 日
The error you got indicates that there may be a problem with the syntax or delimiters in your code. To resolve this error, look for any mismatched parentheses or brackets in your code.
According to the code sample you gave, the problem appears to be occurring in one of the methods you are calling i.e. VesselEvacuation or CCVP. Without the full script, I can’t pinpoint exactly what may be going wrong. However, when invoking these functions, ensure that you use the right syntax and that the functions are properly defined.
Here are a few things you can do to fix the problem:
  1. Look at the function definitions: Check that the functions VesselEvacuationand CCVP have the correct amount of input arguments and output variables.
  2. Examine the function calls: When invoking the functions, make sure you use the correct syntax. If there are several arguments, the function name should be followed by parenthesis (), and the input arguments should be provided within the parentheses, separated by commas.
  3. Examine the variable names: Before calling the functions, ensure that all variables (P_intial, S, LR, radius, height, Vol_sample, time_plot, vessel”, etc.) are properly declared and have the correct values allocated to them.
You should be able to discover and rectify any errors producing the "Invalid expression" error in your code by carefully reviewing the syntax, function definitions, and variable names.
Hope this helps!


Help Center および File ExchangeLoops and Conditional Statements についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by