automaticaly define calibration parameter to the workspace through read the m file
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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.
Accepted Answer
Mathieu NOE
on 20 Oct 2023
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 Comments
wenchao zhang
on 21 Oct 2023
thanks, Mathieu, yes i tryed this on my laptop, it works, this information can help me to creat the calibration parameter to the workspace.

wenchao zhang
on 21 Oct 2023
another question, we know s{1} = EngDa_jEng_C, i want implement(EngDa_jEng_C = Simulink.Parameter) in script, how to do?
Mathieu NOE
on 23 Oct 2023
hello
what is "Simulink.Parameter" ? is it a field (and Simulink a structure) ? or just a string or char array ?
wenchao zhang
on 23 Oct 2023
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]
wenchao zhang
on 23 Oct 2023
Edited: wenchao zhang
on 23 Oct 2023
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]
wenchao zhang
on 23 Oct 2023
Mathieu NOE
on 23 Oct 2023
My pleasure !
More Answers (0)
Categories
Find more on Event Functions in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)