how to load the .dat file using user define block?

조회 수: 6 (최근 30일)
Vims
Vims 2024년 12월 25일
댓글: Walter Roberson 2024년 12월 31일
I have user define matlab function are as attached. Now I dont need any input port but only one output port . How can my userdefine block capture the respective file and load the data and read it.
I have wrote code in matlab file but dont know in simulink version.
imuFileName = 'IMU_19.dat'; % Example IMU data file (modify as needed)
fid = fopen(imuFileName, 'r');
if fid == -1
error('Cannot open the file "%s".', imuFileName);
end
try
% We assume 'IMU_19.dat' has 10 columns with 10,000 rows:
% Time, <unused>, AccX, AccY, AccZ, GyroX, GyroY, GyroZ, <unused>, <unused>
rawData = textscan(fid, '%f %f %f %f %f %f %f %f %f %f', ...
'Delimiter', {' ', '\t'}, 'MultipleDelimsAsOne', true);
fclose(fid);
catch ME
fclose(fid);
error('Error reading IMU file: %s', ME.message);
end
IMU_data = cell2mat(rawData);
[numRows, numCols] = size(IMU_data);
if (numRows ~= 10000 || numCols ~= 10)
error('Unexpected IMU data dimensions: %d rows x %d cols (expected 10000 x 10)', ...
numRows, numCols);
end
% -------------------- Basic Extraction -----------------
IMU.Time = IMU_data(:, 1); % 1) Time
IMU.Acc_X = IMU_data(:, 3); % 3) Acc X
IMU.Acc_Y = IMU_data(:, 4); % 4) Acc Y
IMU.Acc_Z = IMU_data(:, 5); % 5) Acc Z
IMU.Gyro_X = IMU_data(:, 6); % 6) Gyro X
IMU.Gyro_Y = IMU_data(:, 7); % 7) Gyro Y
IMU.Gyro_Z = IMU_data(:, 8); % 8) Gyro Z
disp('IMU data structure created successfully.');
  댓글 수: 2
Arjun
Arjun 2024년 12월 31일
Hi @Vims,
One way this can be done is to load data to the workspace and then use the 'From Workspace' block in Simulink to fetch the required variables for use.
Another way can be to save data as a .MAT file and then use the 'From File' block in Simulink to read data.
Refer to the following documentation for 'From File' Block: https://www.mathworks.com/help/simulink/ug/load-data-using-the-from-file-block.html
I hope this helps!
Walter Roberson
Walter Roberson 2024년 12월 31일
Question:
Do you need the output to be parceled out over time, the first being at IMU.Time(1) with corresponding Acc_X(1), Acc_Y(1) and so on, and the second being at time IMU.Time(2) with corresponding Acc_X(2), Acc_Y(2) and so on?
Or do you need the entire block of data at the same time? If so then it as-if a constant block, or is the IMU file changing during execution ?

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

답변 (1개)

Malay Agarwal
Malay Agarwal 2024년 12월 31일
편집: Malay Agarwal 2024년 12월 31일
Hi @Vims,
You can use a MATLAB Function block.
Refer to the following documentation for more information on the MATLAB Function Block: https://www.mathworks.com/help/simulink/slref/matlabfunction.html.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by