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

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 commentaires

Arjun
Arjun le 31 Déc 2024
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!
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 ?

Connectez-vous pour commenter.

Réponses (1)

Malay Agarwal
Malay Agarwal le 31 Déc 2024
Modifié(e) : Malay Agarwal le 31 Déc 2024

0 votes

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!

Catégories

En savoir plus sur Simulink dans Centre d'aide et File Exchange

Produits

Version

R2024a

Question posée :

le 25 Déc 2024

Commenté :

le 31 Déc 2024

Community Treasure Hunt

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

Start Hunting!

Translated by