end command doen't end my function

2 ビュー (過去 30 日間)
M.
M. 2023 年 8 月 30 日
コメント済み: M. 2023 年 8 月 31 日
Hi! I'm trying to understand programmatic modelling and I'm reading this documentation.
The code I'm running is as follows:
clc
clear
close all
function new(modelname)
% NEW_MODEL Create a new, empty Simulink model
% NEW_MODEL('MODELNAME') creates a new model with
% the name 'MODELNAME'. Without the 'MODELNAME'
% argument, the new model is named 'my_untitled'.
if nargin == 0
modelname = 'my_untitled';
end
load_system('simulink')
% create and open the model
open_system(new_system(modelname));
% set default solver
set_param(modelname,'Solver','ode3');
add_block('simulink/Sources/Sine Wave','mymodel/Sine1');
set_param('mymodel/Sine1','position',[140,80,180,120]);
add_block('simulink/Sources/Pulse Generator','mymodel/Pulse1');
set_param('mymodel/Pulse1','position',[140,200,180,240]);
add_block('simulink/Ports & Subsystems/Subsystem','mymodel/Subsystem1');
set_param('mymodel/Subsystem1','position',[315,120,395,200]);
add_block('simulink/Sinks/Scope','mymodel/Scope1');
set_param('mymodel/Scope1','position',[535,140,575,180]);
% save the model
save_system(modelname);
I'm getting this error: Error: File: new_model.m Line: 35 Column: 1
All functions in a script must be closed with an 'end'.
I tried adding a second end after the first one, but then I got this error: Error: File: new_model.m Line: 16 Column: 1
Function definitions in a script must appear at the end of the
file.
Move all statements after the "new" function definition to
before the first local function definition.
I'm not quite sure what this means. What could it be?
  2 件のコメント
Stephen23
Stephen23 2023 年 8 月 30 日
"What could it be?"
These awful, counter-productive, anti-pattern, cargo-cult progamming lines of code here:
clc
clear
close all
Get rid of them.
M.
M. 2023 年 8 月 31 日
Thank you. They were indeed causing a problem

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

採用された回答

Walter Roberson
Walter Roberson 2023 年 8 月 30 日
Remove the lines
clc
clear
close all
They are not doing any good there, only causing problems.
The only time you should ever have close all inside a script, is if you create a script designed to reinitialize MATLAB when you switch what you are doing -- code intended to act as shortcuts for quitting MATLAB and starting it again.
  1 件のコメント
M.
M. 2023 年 8 月 31 日
Thank you for the explanation! They were the problem with the code

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

その他の回答 (1 件)

YASSINE
YASSINE 2023 年 8 月 30 日
編集済み: YASSINE 2023 年 8 月 30 日
Hi, its a function you should create a function called "new_model" that has the code described above and save it in your working directory. then call it in the command window
>> new_model()
by the way, "mymodel" in the your code should match the modelname
  1 件のコメント
M.
M. 2023 年 8 月 31 日
Thank you! It was mismatched

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

カテゴリ

Help Center および File ExchangeSources についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by