Replace values in table from a second table

12 visualizaciones (últimos 30 días)
Rodolfo Villanueva
Rodolfo Villanueva el 17 de En. de 2019
Respondida: Peter Perkins el 23 de En. de 2019
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:
  1. 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.
  2. 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 comentarios
Rodolfo Villanueva
Rodolfo Villanueva el 18 de En. de 2019
It means a table object
Jan
Jan el 18 de En. de 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?

Iniciar sesión para comentar.

Respuestas (2)

madhan ravi
madhan ravi el 17 de En. de 2019
  2 comentarios
Rodolfo Villanueva
Rodolfo Villanueva el 18 de En. de 2019
Using strcmp would mean to write endless lines of code and would probably very slow.
Jan
Jan el 18 de En. de 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.

Iniciar sesión para comentar.


Peter Perkins
Peter Perkins el 23 de En. de 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"

Categorías

Más información sobre Interactive Model Editing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by