Replace values in table from a second table

조회 수: 6 (최근 30일)
Rodolfo Villanueva
Rodolfo Villanueva 2019년 1월 17일
답변: Peter Perkins 2019년 1월 23일
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
Rodolfo Villanueva
Rodolfo Villanueva 2019년 1월 18일
It means a table object
Jan
Jan 2019년 1월 18일
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

madhan ravi
madhan ravi 2019년 1월 17일
  댓글 수: 2
Rodolfo Villanueva
Rodolfo Villanueva 2019년 1월 18일
Using strcmp would mean to write endless lines of code and would probably very slow.
Jan
Jan 2019년 1월 18일
@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
Peter Perkins 2019년 1월 23일
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"

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by