Seeking advice from experts

32 views (last 30 days)
杰
on 16 Nov 2025 at 5:33
Edited: Stephen23 on 17 Nov 2025 at 13:59
clear all
close all
clc
all_files = dir('packing_*h5');
inpfile='input-sandpile.inp';
% 创建两个空结构数组来存放分类后的文件
df_normal = [];
df_bf = [];
for i = 1:length(all_files)
if contains(all_files(i).name, '_bf_') % 检查是否包含 '_bf_' 这个模式
df_bf = [df_bf; all_files(i)];
else
df_normal = [df_normal; all_files(i)];
end
end
function [dtOut2,R] = extractParams(filename)
text = fileread(filename);
dtOut2_match = regexp(text, '([0-9.eE+-]+)\s*=\s*dtOut2', 'tokens', 'once');
if ~isempty(dtOut2_match)
dtOut2 = str2double(dtOut2_match{1});
end
R_match = regexp(text, '([0-9.eE+-]+)\s*=\s*R', 'tokens', 'once');
if ~isempty(R_match)
R = str2double(R_match{1});
end
end
function [PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames)
pos = h5read(filenames,'/Position');
PTag = h5read(filenames,'/PTag');
vp = h5read(filenames,'/PVelocity');
PR = h5read(filenames,'/Radius');
PTag = h5read(filenames,'/PTag');
F = h5read(filenames,'/PUForce');
xp = pos(1:3:end);
yp = pos(2:3:end);
zp = pos(3:3:end);
vpx = vp(1:3:end);
vpy = vp(2:3:end);
vpz = vp(3:3:end);
Fx = F(1:3:end);
Fy = F(2:3:end);
Fz = F(3:3:end);
end
[dtOut2,R] = extractParams(inpfile);
stepp=500:1:600;
num_step=0;
for i=stepp
filenames = df_normal(i).name;
%filenamebf = dfbf(i).name;
clear PTag PR xp yp zp vpx vpy vpz Fx Fy Fz
[PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames);
numabove=0;
for j=1:length(zp)
if PTag(j)==-1&&zp(j)>=0.1
numabove=numabove+1;
end
end
Time(i)=dtOut2*i;
Packing(i)=numabove
end
lmw=1.5;
MS=7;
frontszie=17;
plot(Time(stepp), Packing(stepp), 'o', 'LineWidth', lmw, 'MarkerSize', MS);
hold on
xlabel('time');ylabel('num','interpreter','latex');
set(gca,'FontSize',frontszie,'FontName','Times New Roman','LineWidth',lmw);
出现如下错误,该怎么解决
How to resolve the following error?
function [dtOut2,R] = extractParams(filename)
错误: 函数定义在此上下文中不受支持。函数只能作为代码文件中的局部函数或嵌套函数创建。
Error: Function definitions are not supported in this context. Functions can only be created as local functions or nested functions within a code file.
  1 Comment
Stephen23
Stephen23 about 8 hours ago
Edited: Stephen23 25 minutes ago
"Error: Function definitions are not supported in this context."
Are you attempting to define these function in the command window?
Note that you can replace this low-level code:
df_normal = [];
df_bf = [];
for i = 1:length(all_files)
if contains(all_files(i).name, '_bf_') % 检查是否包含 '_bf_' 这个模式
df_bf = [df_bf; all_files(i)];
else
df_normal = [df_normal; all_files(i)];
end
end
with this simpler and more efficient MATLAB code:
idx = contains({all_files(i).name}, '_bf_') % 检查是否包含 '_bf_' 这个模式
df_bf = all_files(idx);
df_normal = all_files(~idx);
Note that your code will throw errors if dtOut2_match or R_match are empty.

Sign in to comment.

Answers (2)

Torsten
Torsten on 16 Nov 2025 at 12:29
Edited: Torsten on 16 Nov 2025 at 14:39
Use one of the below codes instead.
You can't nest functions within a script, but only within another function. So either define the script as a function or put the functions to the end of your code.
First alternative:
main()
function main
clear all
close all
clc
all_files = dir('packing_*h5');
inpfile='input-sandpile.inp';
% 创建两个空结构数组来存放分类后的文件
df_normal = [];
df_bf = [];
for i = 1:length(all_files)
if contains(all_files(i).name, '_bf_') % 检查是否包含 '_bf_' 这个模式
df_bf = [df_bf; all_files(i)];
else
df_normal = [df_normal; all_files(i)];
end
end
function [dtOut2,R] = extractParams(filename)
text = fileread(filename);
dtOut2_match = regexp(text, '([0-9.eE+-]+)\s*=\s*dtOut2', 'tokens', 'once');
if ~isempty(dtOut2_match)
dtOut2 = str2double(dtOut2_match{1});
end
R_match = regexp(text, '([0-9.eE+-]+)\s*=\s*R', 'tokens', 'once');
if ~isempty(R_match)
R = str2double(R_match{1});
end
end
function [PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames)
pos = h5read(filenames,'/Position');
PTag = h5read(filenames,'/PTag');
vp = h5read(filenames,'/PVelocity');
PR = h5read(filenames,'/Radius');
PTag = h5read(filenames,'/PTag');
F = h5read(filenames,'/PUForce');
xp = pos(1:3:end);
yp = pos(2:3:end);
zp = pos(3:3:end);
vpx = vp(1:3:end);
vpy = vp(2:3:end);
vpz = vp(3:3:end);
Fx = F(1:3:end);
Fy = F(2:3:end);
Fz = F(3:3:end);
end
[dtOut2,R] = extractParams(inpfile);
stepp=500:1:600;
num_step=0;
for i=stepp
filenames = df_normal(i).name;
%filenamebf = dfbf(i).name;
clear PTag PR xp yp zp vpx vpy vpz Fx Fy Fz
[PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames);
numabove=0;
for j=1:length(zp)
if PTag(j)==-1&&zp(j)>=0.1
numabove=numabove+1;
end
end
Time(i)=dtOut2*i;
Packing(i)=numabove
end
lmw=1.5;
MS=7;
frontszie=17;
plot(Time(stepp), Packing(stepp), 'o', 'LineWidth', lmw, 'MarkerSize', MS);
hold on
xlabel('time');ylabel('num','interpreter','latex');
set(gca,'FontSize',frontszie,'FontName','Times New Roman','LineWidth',lmw);
end
Second alternative:
clear all
close all
clc
all_files = dir('packing_*h5');
inpfile='input-sandpile.inp';
% 创建两个空结构数组来存放分类后的文件
df_normal = [];
df_bf = [];
for i = 1:length(all_files)
if contains(all_files(i).name, '_bf_') % 检查是否包含 '_bf_' 这个模式
df_bf = [df_bf; all_files(i)];
else
df_normal = [df_normal; all_files(i)];
end
end
[dtOut2,R] = extractParams(inpfile);
stepp=500:1:600;
num_step=0;
for i=stepp
filenames = df_normal(i).name;
%filenamebf = dfbf(i).name;
clear PTag PR xp yp zp vpx vpy vpz Fx Fy Fz
[PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames);
numabove=0;
for j=1:length(zp)
if PTag(j)==-1&&zp(j)>=0.1
numabove=numabove+1;
end
end
Time(i)=dtOut2*i;
Packing(i)=numabove
end
lmw=1.5;
MS=7;
frontszie=17;
plot(Time(stepp), Packing(stepp), 'o', 'LineWidth', lmw, 'MarkerSize', MS);
hold on
xlabel('time');ylabel('num','interpreter','latex');
set(gca,'FontSize',frontszie,'FontName','Times New Roman','LineWidth',lmw);
function [dtOut2,R] = extractParams(filename)
text = fileread(filename);
dtOut2_match = regexp(text, '([0-9.eE+-]+)\s*=\s*dtOut2', 'tokens', 'once');
if ~isempty(dtOut2_match)
dtOut2 = str2double(dtOut2_match{1});
end
R_match = regexp(text, '([0-9.eE+-]+)\s*=\s*R', 'tokens', 'once');
if ~isempty(R_match)
R = str2double(R_match{1});
end
end
function [PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames)
pos = h5read(filenames,'/Position');
PTag = h5read(filenames,'/PTag');
vp = h5read(filenames,'/PVelocity');
PR = h5read(filenames,'/Radius');
PTag = h5read(filenames,'/PTag');
F = h5read(filenames,'/PUForce');
xp = pos(1:3:end);
yp = pos(2:3:end);
zp = pos(3:3:end);
vpx = vp(1:3:end);
vpy = vp(2:3:end);
vpz = vp(3:3:end);
Fx = F(1:3:end);
Fy = F(2:3:end);
Fz = F(3:3:end);
end

Image Analyst
Image Analyst on 16 Nov 2025 at 15:44
What you have is essentially this
% Initial script
% code.....
%-------------------------------------------------------------------
% define function 1
function [dtOut2,R] = extractParams(filename)
% Code for function 1
end % of function 1 definition.
%-------------------------------------------------------------------
% define function 2
function [PR, PTag, xp, yp, zp, vpx, vpy, vpz, Fx, Fy,Fz] = extractMechsysdata(filenames)
% Code for function 2
end % of function 2 definition
%-------------------------------------------------------------------
% Initial script starts up again (not allowed)
% Remaining code for initial script follows.....
So you see you have a script with functions defined in the middle of the script. That's not allowed. Simply put all your script at the top, then follow it with as many function definitions as you want, with each function closing with an "end" statement.
Also, your code needs a lot more comments so others can understand what's going on and even for you to understand what's going on if you look at it much later. Comments are needed to write maintainable code.
  1 Comment
Walter Roberson
Walter Roberson about 16 hours ago
Edited: Walter Roberson less than a minute ago
Before R2024a: Local functions in scripts must be defined at the end of the file, after the last line of script code.
So the above structure is legal these days. But I suspect that the original poster is using something before R2024a. Either that or the original poster is trying to paste everything at the command prompt instead of in a file.

Sign in to comment.

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!