automaticaly define calibration parameter to the workspace through read the m file

i have a m file, it defineed many calibration parameters one by one, each line seems like following: objDef_CAL('EngDa_jEng_C', '0.197350', 'Min', '0', 'Max', '10', 'Width', '1', 'Typedef', 'j_kgm2_t', 'Rate', 'MED','Description', 'engine inertia'); First question:how to get different part through each line, for example(Name,Min,Max,width...) second: how to define the calibration parameter to worksapce use script.

 채택된 답변

hello
maybe this ?
see my dummy calibration file attached (it's a txt and not a m file)
I simply created a second line with slightly different values (to test the code)
the code : will generate a "out" structure with fields :
out(1) = struct with fields:
EngDa_jEng_C: 0.1973
Min: 0
Max: 10
Width: 1
Typedef: ' j_kgm2_t'
Rate: ' MED'
Description: ' engine inertia'
out(2) = struct with fields:
EngDa_jEng_C: 0.4974
Min: 0
Max: 20
Width: 3
Typedef: ' j_kgm2_t'
Rate: ' MED'
Description: ' engine inertia'
D=readlines('calibration.txt'); % read as string array
lines=find(contains(D,'objDef_CAL')); % find the "objDef_CAL" line(s)
k = 0;
for ck=1:numel(lines)
tmp = char(D(lines(ck)));
% remove quote character
iq = findstr(tmp,'''');
tmp(iq) = [];
tmp = extractBetween(tmp,'(',')');
% check if line is empty or not
if ~isempty(tmp)
k = k+1;
s =split(tmp, ',');
% fill the structure "out" with info's
out(k).EngDa_jEng_C = str2double(s{2});
out(k).Min = str2double(s{4});
out(k).Max = str2double(s{6});
out(k).Width = str2double(s{8});
out(k).Typedef = s{10};
out(k).Rate = s{12};
out(k).Description = s{14};
end
end
% let's see what we have
out(1)
out(2)

댓글 수: 9

thanks, Mathieu, yes i tryed this on my laptop, it works, this information can help me to creat the calibration parameter to the workspace.
another question, we know s{1} = EngDa_jEng_C, i want implement(EngDa_jEng_C = Simulink.Parameter) in script, how to do?
hello
what is "Simulink.Parameter" ? is it a field (and Simulink a structure) ? or just a string or char array ?
Simulink.Parameter is a command, if i run the command(a =Simulink.Parameter),then a parameter called a will be created,like following:
a =
Parameter with properties:
Value: []
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'auto'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [0 0]
now i want to creat a parameter named(EngDa_jEng_C), i want realize this in script(now we know s{1} = 'EngDa_jEng_C'), how to do?
I suspect you want to dynamically create a variable named EngDa_jEng_C and assign to it the structure content of Simulink.Parameter ?
I wouldsuggest you to read this :
instead , based on the code I suggested above , we can expand it to have the "out" structure have now more fields like :
out.name = 'EngDa_jEng_C
out.param = parameter structure from "Simulink.Parameter"
let's try it (I have run a simulink file of mine and I have this data in my workspace :
a =Simulink.Parameter
a = Parameter with properties:
Value: []
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'auto'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [0 0]
now let's try to implement this in the code :
D=readlines('calibration.txt'); % read as string array
lines=find(contains(D,'objDef_CAL')); % find the "objDef_CAL" line(s)
k = 0;
for ck=1:numel(lines)
tmp = char(D(lines(ck)));
% remove quote character
iq = findstr(tmp,'''');
tmp(iq) = [];
tmp = extractBetween(tmp,'(',')');
% check if line is empty or not
if ~isempty(tmp)
k = k+1;
s =split(tmp, ',');
% fill the structure "out" with info's
out(k).name = s{1};
out(k).param = Simulink.Parameter;
out(k).Min = str2double(s{4});
out(k).Max = str2double(s{6});
out(k).Width = str2double(s{8});
out(k).Typedef = s{10};
out(k).Rate = s{12};
out(k).Description = s{14};
end
end
% let's see what we have
out(1)
out(2)
the two out structure will be :
out(1) = struct with fields:
name: 'EngDa_jEng_C'
param: [1×1 Simulink.Parameter]
Min: 0
Max: 10
Width: 1
Typedef: ' j_kgm2_t'
Rate: ' MED'
Description: ' engine inertia'
out(2) = struct with fields:
name: 'EngDa_jEng_C'
param: [1×1 Simulink.Parameter]
Min: 0
Max: 20
Width: 3
Typedef: ' j_kgm2_t'
Rate: ' MED'
Description: ' engine inertia'
and if we look closer into first out fields (out(1).param) we have your Simulink.Parameter datas
out(1).param = Parameter with properties:
Value: []
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'auto'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [0 0]
yes, good idea, then each element of out will have the parameter of(Simulink.Parameter), but if i can directly to see the name(EngDa_jEng_C) in workspace, it will be better, because this is clear, like following,
ok, not recommended but you can use assignin for that purpose
see new code below :
D=readlines('calibration.txt'); % read as string array
lines=find(contains(D,'objDef_CAL')); % find the "objDef_CAL" line(s)
k = 0;
for ck=1:numel(lines)
tmp = char(D(lines(ck)));
% remove quote character
iq = findstr(tmp,'''');
tmp(iq) = [];
tmp = extractBetween(tmp,'(',')');
% check if line is empty or not
if ~isempty(tmp)
k = k+1;
s =split(tmp, ',');
% fill the structure "out" with info's
% out(k).name = s{1};
% out(k).param = Simulink.Parameter;
out(k).Min = str2double(s{4});
out(k).Max = str2double(s{6});
out(k).Width = str2double(s{8});
out(k).Typedef = s{10};
out(k).Rate = s{12};
out(k).Description = s{14};
% specifically assign "Simulink.Parameter" to s{1} (i.e.var name EngDa_jEng_C)
assignin ( 'caller', s{1}, Simulink.Parameter );
end
end
now you have in the workspace a new varaible EngDa_jEng_C (from s{1}) and we have assigned to it the output of Simulink.Parameter
EngDa_jEng_C = Value: []
CoderInfo: [1×1 Simulink.CoderInfo]
Description: ''
DataType: 'auto'
Min: []
Max: []
Unit: ''
Complexity: 'real'
Dimensions: [0 0]

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Event Functions에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2023년 10월 20일

댓글:

2023년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by