Seeking advice from experts

조회 수: 33 (최근 30일)
杰
2025년 11월 16일 5:33
편집: Stephen23 2025년 11월 17일 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
Stephen23
Stephen23 2025년 11월 17일 6:16
편집: Stephen23 2025년 11월 17일 13:59
"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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Torsten
Torsten 2025년 11월 16일 12:29
편집: Torsten 2025년 11월 16일 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 2025년 11월 16일 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
Walter Roberson
Walter Roberson 2025년 11월 16일 21:15
편집: Walter Roberson 2025년 11월 17일 12:56
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by