Replace values in table from a second table
18 views (last 30 days)
Show older comments
I have a variable-sized table, created automatically by a simulink callback at the end of a simulation. This extracts all the logged signals, and creates a table with their corresponding variable names.
After this, I need to replace certain values with certain strings. The idea is to have a second table with two columns: First column is a number, second column is a text string.
I need to write a code that:
- Reads all the numbers in column 1 of the second table and sees if there is a match in all the cells of table number one.
- If there are matches, to replace the value found in table number one with the text string from table number two that shares the row of the reference value.
I have been trying some combination of codes, but failed miserably so far.
Any thoughts on this would be great.
Thanks!
3 Comments
Jan
on 18 Jan 2019
Please post a small example for the inputs and wanted output. I'm confused by this detail: "The idea is to have a second table with two columns: First column is a number, second column is a text string." As far as I understand, you do not have the 2nd table, do you? Then How can you read all numbers in its first column? What should happen, when a number is not found?
Answers (2)
madhan ravi
on 17 Jan 2019
help strcmp
2 Comments
Jan
on 18 Jan 2019
@Rodolfo Villanueva: I do not think so. Because strcmp is very fast and I assume a small loop is sufficient already, I expect this is an efficient solution.
Peter Perkins
on 23 Jan 2019
This sounds like in general you'd want a join operation. Hard to say without details.
>> t1 = table([1;2;3;4],["a"; "b"; "c"; "d"])
t1 =
4×2 table
Var1 Var2
____ ____
1 "a"
2 "b"
3 "c"
4 "d"
>> t2 = table([2;4;6],["bb"; "dd"; "ff"])
t2 =
3×2 table
Var1 Var2
____ ____
2 "bb"
4 "dd"
6 "ff"
>> t3 = outerjoin(t1,t2,'Key','Var1','Type','left')
t3 =
4×4 table
Var1_t1 Var2_t1 Var1_t2 Var2_t2
_______ _______ _______ _________
1 "a" NaN <missing>
2 "b" 2 "bb"
3 "c" NaN <missing>
4 "d" 4 "dd"
>> fromT2 = ~isnan(t3.Var1_t2)
fromT2 =
4×1 logical array
0
1
0
1
>> t1.Var2(fromT2) = t3.Var2_t2(fromT2)
t1 =
4×2 table
Var1 Var2
____ ____
1 "a"
2 "bb"
3 "c"
4 "dd"
0 Comments
See Also
Categories
Find more on Simulink Functions 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!