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

조회 수: 5 (최근 30일)
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.

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 10월 20일
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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by