Error: Function definitions are not permitted in this context
Mostra commenti meno recenti
function s = myode_detuning(t, G, omega_e,omega_c, Omega, gamma, topt, tau_opt, tspan, gammafunc)
gammafunc = interp1(tspan,gammafunc,t); % Interpolate the data set (ts,gammafunc) at time t
s = [G(2) ; -omega_e^2 * G(1) + Omega^2 * G(3) - gammafunc * G(2) + sin(omega_c*t)*exp(-4*log(2)*(t-topt)^2/tau_opt^2); G(4) ; -omega_c^2 * G(3) + Omega^2 * G(1) - gamma * G(4)] ;
I run the code above on MATLAB R2016b to define function that's used in another MATLAB script (separate file).
tspan is defined in that another file.
However, I get errors below.
{Error: Function definitions are not permitted in this context.
}
{Undefined function or variable 'tspan'.
}
{Undefined function or variable 'G'.
}
When I run this code using MATLAB R2019a it works fine.
What do I need to fix in order to run this in R2016b?
The code above is saved in a (.m) file.
4 Commenti
Adam Danz
il 14 Apr 2020
Please remember to provide the entire error message when reporting errors.
Mai Sakuragi
il 14 Apr 2020
Mai Sakuragi
il 14 Apr 2020
Risposte (1)
Starting in r2016b, functions can be defined within scripts but they must end with the end keyword.
For more information:
4 Commenti
Mai Sakuragi
il 14 Apr 2020
Hmmm... I'm not familiar with the Microsoft HPC Pack. Nevertheless, I notice two things.
1) The attached file is a function, not a script, so my answer doesn't address the problem if that's the complete file. Adding the 'end' keyword shouldn't cause an error wither the file is a function or a script.
2) The 1st screenshot under your question differs from the 2nd screenshot above. In the 1st screenshot I see the function definition line and the 'end' keyword but nothing between. I don't see anything in the 2nd screenshot (or the attached file) that would cause the errors you shared but it wouldn't surprise me if the function in the 1st screenshot threw errors.
Again, I'm unfamiliar with the HPC pack so there might be something I'm missing.
Mai Sakuragi
il 14 Apr 2020
Mai Sakuragi
il 14 Apr 2020
Categorie
Scopri di più su Introduction to Installation and Licensing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

