What is valid type?

6 views (last 30 days)
Vims
Vims on 25 Dec 2024
Commented: Vims on 28 Dec 2024
Expression 'IMU_Bus' for type of data 'imu_data_struct' did not evaluate to a valid type.
Caused by:
Invalid setting in 'MATLAB Function' for parameter 'Datatype'
Error evaluating MATLAB Function parameter data 'Datatype' in its parent workspace.
Unrecognized function or variable 'IMU_Bus'.
Variable 'IMU_Bus' does not exist.
Suggested Actions:
Load a file into base workspace. - Fix
Create a new variable. - Fix
I am not able to understand this error as I have below file when i load it then it works but when i run simulation then shows same error.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File: read_imu_data.m
% Description:
% Reads custom IMU file and returns a structured array containing raw
% IMU increments, message counters, sample times, etc.
%
% Guidelines:
% - snake_case for function and variables
% - no global usage
% - columns are identified and documented
%
% Author: <Your Name>
% Date: <Date>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imu_data_struct = read_imu_data(file_name)
% READ_IMU_DATA Reads the specified IMU file and parses data columns.
%
% INPUT:
% file_name [string] - path to the IMU data file (no spaces).
%
% OUTPUT:
% imu_data_struct - a structure with fields:
% .msg_counter : message counter [0..255]
% .sample_time_s : sample time in seconds [resets at each PPS]
% .gyro_x_inc : gyro X increments
% .gyro_y_inc : gyro Y increments
% .gyro_z_inc : gyro Z increments
% .acc_x_inc : accelerometer X increments
% .acc_y_inc : accelerometer Y increments
% .acc_z_inc : accelerometer Z increments
% .temperature : sensor temperature (unit depends on sensor)
% .status_code : status code (unitless)
%
% Example usage:
% imu_data = read_imu_data('IMU_19.dat');
fid = fopen(file_name, 'r');
if fid == -1
error('read_imu_data:CannotOpenFile', ...
'Could not open IMU file: %s', file_name);
end
data_array = textscan(fid, '%f %f %f %f %f %f %f %f %f %f', ...
'Delimiter',' ', 'MultipleDelimsAsOne', true);
fclose(fid);
data_matrix = cell2mat(data_array);
% Parse columns into a structured form
imu_data_struct.msg_counter = data_matrix(:,1);
imu_data_struct.sample_time_s = data_matrix(:,2);
imu_data_struct.gyro_x_inc = data_matrix(:,3);
imu_data_struct.gyro_y_inc = data_matrix(:,4);
imu_data_struct.gyro_z_inc = data_matrix(:,5);
imu_data_struct.acc_x_inc = data_matrix(:,6);
imu_data_struct.acc_y_inc = data_matrix(:,7);
imu_data_struct.acc_z_inc = data_matrix(:,8);
imu_data_struct.temperature = data_matrix(:,9);
imu_data_struct.status_code = data_matrix(:,10);
end
  2 Comments
Shivam Gothi
Shivam Gothi on 26 Dec 2024
Hello @Vims,
I am not able to reproduce the issue from my end.
The simulink model contains the scripts "load_IMU_data" and "load_IMU_data_simple". Can you please share those files if possible ? Also, I investigated that the model reads the sensor data from a text file. Can you also provide a sample file ?
It will be helpful in reproducing the issue and suggesting proper solutions.

Sign in to comment.

Answers (1)

Shivam Gothi
Shivam Gothi on 26 Dec 2024
Hello @Vims,
I understand that you are trying to run the attached simulink model, but facing some error. Basically, you want to use the structure named "imu_data_struct" defined in the base workspace, in your simulink model.
You can achieve this using the below suggested work-around.
You need not to have an additional "input" port in your MATLAB function block to get the structure "imu_data_struct" as an input data.
Instead of it, you can define "imu_data_struct", appearing in all of the MATLAB function blocks as "parameter data" in the symbols pane. This will not create an input port, but will directly fetch the value of "imu_data_struct" from the base workspace (if it is defined there). Follow the below steps to define "imu_data_struct" as "parameter data":
  • Double click on the MATLAB function block. This will open the editor.
  • Navigate to the "Modelling" tab in the tool-strip and click the "Symbols Pane" icon. This will open the symbols pane on the right hand side of the window as shown in the below figure.
  • Click the symbol appearing besides the "imu_data_struct" in the symbols pane and change the "type" to "parameter data" as shown in below figure.
  • Now, your MATLAB function block will not have any "input port" as seen below:
This will also resolve the error attached by you in the screen-shot.
NOTE: YOU FIRST NEED TO DEFINE "imu_data_struct" IN THE BASE WORKSPACE AND ALSO ASSIGN PROPER VALUES TO ITS ELEMENTS.
You can achieve this by typing the below lines of code in the "PreLoadFcn" callback appearing in the "Model properties" under the "Modelling" tab.
%The below lines of code will read the IMU data file and stores the values
%in appropriate fields of structure.
file_name="IMU_sample_data.txt";
T=readtable("IMU_sample_data.txt");
data_array=table2array(T);
data_matrix = data_array;
% Parse columns into a structured form
imu_data_struct.msg_counter = data_matrix(:,1);
imu_data_struct.sample_time_s = data_matrix(:,2);
imu_data_struct.gyro_x_inc = data_matrix(:,3);
imu_data_struct.gyro_y_inc = data_matrix(:,4);
imu_data_struct.gyro_z_inc = data_matrix(:,5);
imu_data_struct.acc_x_inc = data_matrix(:,6);
imu_data_struct.acc_y_inc = data_matrix(:,7);
imu_data_struct.acc_z_inc = data_matrix(:,8);
imu_data_struct.temperature = data_matrix(:,9);
imu_data_struct.status_code = data_matrix(:,10);
This will create the structure "imu_data_struct" in the base workspace whenever you open the simulink model.
I hope you find this useful !
  3 Comments
Vims
Vims on 27 Dec 2024
https://github.com/VimsRocz/Project here is the repository
Vims
Vims on 28 Dec 2024
I have resolve the issue with data loading which is Task_1 folder of github
read the read_me_file and proceed it. Thanks
Close this issue

Sign in to comment.

Categories

Find more on Data Type Identification 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!