Replacing Data by variable name
Show older comments
Hi All
I have two data set as below
A =
Name subName Type Output Q1 Q2 Q3 Q4
x y xy Person 3 4 5 6
a b ab Animal 2 3 4 5
m n mn Bird 1 2 3 4
s t st None 9 9 9 9
x y NA All 1 1 1 1
B =
Name subName Type Output Q2 Q3
x y xy Person -1 -1
m n mn Bird -2 -2
and I want to replace A's value for given qtrs in B repalced with A so that my new set is
C =
Name subName Type Output Q1 Q2 Q3 Q4
x y xy Person 3 -1 -1 6
a b ab Animal 2 3 4 5
m n mn Bird 1 -2 -2 4
s t st None 9 9 9 9
x y NA All 1 1 1 1
I tried using replacedata with variable names but it gives me error.
I am using Matlab 2013b
3 Comments
Neesha
on 12 Jun 2014
Azzi Abdelmalek
on 12 Jun 2014
Edited: Azzi Abdelmalek
on 12 Jun 2014
What are the classes of your data, cell or dataset?
Neesha
on 12 Jun 2014
Accepted Answer
More Answers (1)
Image Analyst
on 12 Jun 2014
0 votes
Do you have R2013b or later? If so, just use a table.
4 Comments
Neesha
on 12 Jun 2014
Neesha
on 12 Jun 2014
Image Analyst
on 12 Jun 2014
Did you use
tableA = readtable(ExcelFileName);
to read the Excel file into a table (a wonderful new data type started with R2013b)? Please attach the workbook if you want people to try things.
First you need to identify which rows need to get replaced. You do that with ismember(). Then it's straightforward and I think would go something like this ( very untested and just off the top of my head )
for currentRow = 1 : length(tableB.Q2)
% Find rows in A that need to be replaced.
rowsToReplace = ismember(tableB.name(currentRow), tableA.name);
% Replace all the rows in A that need replacing with the value from B.
for rowsInA = find(rowsToReplace)
tableA.Q2(rowsInA) = tableB.Q2(currentRow);
tableA.Q3(rowsInA) = tableB.Q3(currentRow);
end
end
You should also check subname.
Neesha
on 12 Jun 2014
Categories
Find more on Calendar in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!