Create Timetable correspond to another timetable

3 views (last 30 days)
Hi, I am trying to create a timetable that correspond to another timetable data that was generated from CAN data.
For example, I have this timetable with 3 variables and different data.
Reading1 = [0; 0; 3; 3; 2; 0];
Reading2 = [0; 0; 1; 1; 1; 0];
Reading3 = [0; 2; 1; 1; 2; 0];
T = table(Reading1,Reading2, Reading3);
Time = seconds(1:1:6);
TT = table2timetable(T,RowTimes=Time)
TT = 6×3 timetable
Time Reading1 Reading2 Reading3 _____ ________ ________ ________ 1 sec 0 0 0 2 sec 0 0 2 3 sec 3 1 1 4 sec 3 1 1 5 sec 2 1 2 6 sec 0 0 0
Then I would like to create another timetable that has string info for each data that may look like this.
Reading1 = ["Unit Off"; "Unit Off"; "Lock On"; "Lock On"; "Lock Off"; "Unit Off"];
Reading2 = ["Engine Off"; "Engine Off"; "Engine On"; "Engine On"; "Engine On"; "Engine Off"];
Reading3 = ["Hydralic On"; "Hydralic Pressed"; "Hydralic Off"; "Hydralic Off"; "Hydralic Pressed"; "Hydralic On"];
T = table(Reading1,Reading2, Reading3);
Time = seconds(1:1:6);
TT = table2timetable(T,RowTimes=Time)
TT = 6×3 timetable
Time Reading1 Reading2 Reading3 _____ __________ ____________ __________________ 1 sec "Unit Off" "Engine Off" "Hydralic On" 2 sec "Unit Off" "Engine Off" "Hydralic Pressed" 3 sec "Lock On" "Engine On" "Hydralic Off" 4 sec "Lock On" "Engine On" "Hydralic Off" 5 sec "Lock Off" "Engine On" "Hydralic Pressed" 6 sec "Unit Off" "Engine Off" "Hydralic On"
I believe there is a way to make this work for each varible. Would it be easier if I create a table that contains all of the string infomation and somehow the timetable can refer to it by using for loop? Not sure how that would work.
Ps. I am trying to use the app designer so I believe that some of the code may not work the same. I may have some additional question regard to it.
  1 Comment
Dyuman Joshi
Dyuman Joshi on 18 Jan 2024
You can define strings corresponding to each column and then use indexing to obtain the desired table.

Sign in to comment.

Accepted Answer

Hassaan
Hassaan on 18 Jan 2024
Edited: Hassaan on 18 Jan 2024
% Original numeric data
Reading1 = [0; 0; 3; 3; 2; 0];
Reading2 = [0; 0; 1; 1; 1; 0];
Reading3 = [0; 2; 1; 1; 2; 0];
T = table(Reading1, Reading2, Reading3);
Time = seconds(1:1:6);
TT = table2timetable(T, 'RowTimes', Time);
% Define mappings for each Reading
mapping1 = {0, "Unit Off"; 3, "Lock On"; 2, "Lock Off"};
mapping2 = {0, "Engine Off"; 1, "Engine On"};
mapping3 = {0, "Hydralic On"; 2, "Hydralic Pressed"; 1, "Hydralic Off"};
% Convert to containers.Map for easier lookup
map1 = containers.Map([mapping1{:,1}], mapping1(:,2));
map2 = containers.Map([mapping2{:,1}], mapping2(:,2));
map3 = containers.Map([mapping3{:,1}], mapping3(:,2));
% Apply mapping to each column and convert cell arrays to string arrays
Reading1Strings = strings(size(TT.Reading1));
Reading2Strings = strings(size(TT.Reading2));
Reading3Strings = strings(size(TT.Reading3));
for i = 1:length(Reading1Strings)
Reading1Strings(i) = map1(TT.Reading1(i));
Reading2Strings(i) = map2(TT.Reading2(i));
Reading3Strings(i) = map3(TT.Reading3(i));
end
% Create a new timetable with string data
TTStrings = timetable(TT.Time, Reading1Strings, Reading2Strings, Reading3Strings, ...
'VariableNames', {'Reading1', 'Reading2', 'Reading3'});
% Display the new timetable
disp(TTStrings);
Time Reading1 Reading2 Reading3 _____ __________ ____________ __________________ 1 sec "Unit Off" "Engine Off" "Hydralic On" 2 sec "Unit Off" "Engine Off" "Hydralic Pressed" 3 sec "Lock On" "Engine On" "Hydralic Off" 4 sec "Lock On" "Engine On" "Hydralic Off" 5 sec "Lock Off" "Engine On" "Hydralic Pressed" 6 sec "Unit Off" "Engine Off" "Hydralic On"
Generate a new timetable TTStrings where each numeric reading in TT is replaced by its corresponding string as per the defined mappings.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
  3 Comments
Hassaan
Hassaan on 18 Jan 2024
@Min For the code provided yes.
  1. Initial Data and Timetable Creation: You start with three numeric data arrays (Reading1, Reading2, Reading3) and create a timetable TT from these readings, associating them with time intervals.
  2. Defining Mappings: For each set of readings, you define a mapping. These mappings are pairs of numeric values and corresponding text descriptions (e.g., for Reading1, 0 maps to "Unit Off").
  3. Conversion to containers.Map Objects: These mappings are then transformed into containers.Map objects (map1, map2, map3). These objects provide an efficient way to look up the textual description associated with each numeric value.
  4. Numeric to String Conversion: You iterate over the readings in TT and use the maps to convert numeric values into strings. This results in three new arrays of strings (Reading1Strings, Reading2Strings, Reading3Strings).
  5. Creating a New Timetable with String Data: A new timetable TTStrings is created. This timetable replaces the numeric data from TT with the corresponding string descriptions.
  6. Displaying the Converted Timetable: Finally, you display TTStrings, which now provides a more interpretable, text-based representation of the original numeric data.
Effectively transforms a timetable of numeric readings into a more readable format with descriptive text.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
Min
Min on 22 Jan 2024
After a couple of trials, I was able to create the timetable corresponds to the data from another table.
I appreciate your help. Thanks!

Sign in to comment.

More Answers (0)

Categories

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