Passing values from an array to a function

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;
end
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 Comments

Why not assign vessel directly from the 1st function call rather than doing what you are doing?
You want to replace n with i in the code, as the loop variable is i. Otherwise the data is just being overwritten
Also, If all values of mass_plot is zero, assign it out of the loop using zeros
Hi,
I noticed the n instead of i issue after posting this, I'll update the code shown here today to correct that as I am still getting the same issue.
I tried to assign it directly however this caused multiple errors and by introducing the line
vessel(1,n) = Pressure_vessel(1,n)
it eliminated the errors
What I meant to say is, is Pressure_vessel going to be used out of this loop?
If not, then elimnate it-
massplot=zeros(1,n);
for i=1:n
vessel(1,i) = VesselEvacuation(P_intial, S, LR, radius, height, Vol_sample, time_plot) * 100; % *100 to convert to Pa
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;
end
Also, which line is your initial error showing at?
Pressure_vessel and vessel are the same value and was just being used to work around an issue that was coming up - I'll give it a go again with the vessel = Pressure_vessel line and subsequent adjustments.
I think I may have managed to solve the issue with a messy solution. Once I've confirmed the results are correct I'll update this post with a neatened solution.
It will be better if you post your whole code.

Sign in to comment.

Answers (1)

Hello!
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!
Thanks,
Varun

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 2 Aug 2022

Answered:

on 1 Sep 2023

Community Treasure Hunt

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

Start Hunting!