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

6 visualizaciones (últimos 30 días)
Vims
Vims el 25 de Dic. de 2024
Comentada: Walter Roberson el 31 de Dic. de 2024
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 comentarios
Arjun
Arjun el 31 de Dic. de 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!
Walter Roberson
Walter Roberson el 31 de Dic. de 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 ?

Iniciar sesión para comentar.

Respuestas (1)

Malay Agarwal
Malay Agarwal el 31 de Dic. de 2024
Editada: Malay Agarwal el 31 de Dic. de 2024
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!

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by