how to load the .dat file using user define block?
2 views (last 30 days)
Show older comments
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 Comments
Arjun
on 31 Dec 2024
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.
Refer to the following documentation for 'From Workspace' Block: https://www.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=from%20workspace&s_tid=srchtitle_support_results_1_from%20workspace
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
on 31 Dec 2024
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 ?
Answers (1)
Malay Agarwal
on 31 Dec 2024
Edited: Malay Agarwal
on 31 Dec 2024
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!
0 Comments
See Also
Categories
Find more on View and Analyze Simulation Results in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!