How do i make a for loop replace values in a table

8 ビュー (過去 30 日間)
Matt Brianik
Matt Brianik 2018 年 1 月 15 日
コメント済み: Matt Brianik 2018 年 1 月 15 日
I want to loop through a series of numbers in a table and replace specific numbers with a letter value in a corresponding table. In the code below my desired output is 3 rows starting off as "1 2 3 4" that ends up as 3 rows "1,BBB,3,DDD"
clear
close all
NumRows=3
MDL= ['AAA';'BBB';'CCC';'DDD']
x=(1:4)
y=(101:104)
TABLE=[x;x;x]
EX=[5;2;7;4]
Collum=1;
Row=1;
k=1;
RowTurnedTable=TABLE(Row,:)
for i=1:NumRows;
if RowTurnedTable(i) == EX(i)
TABLE(i,:)=MDL(i)
else TABLE(i,:) = TABLE(i,:)
end
end
I know this is probably a simple solution but i'm struggling.
My 2 big questions are how do i loop it through to replace the correct numbers with the correct letters, and how do i make it replace actual letters because it is replacing numbers with the numeric code for the letters indicated above, EX: AAA=67?

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 1 月 15 日
All of the entries for a single variable in a table must be the same data type. It is not possible to have some entries be numeric and others be character. Because of that it is not possible to replace the numeric entries by characters one by one.
You want to have a mix of numeric and character for entries. That is not directly possible. You could replace all of the entries changing the numeric ones to character vectors representing the numbers; or you could change all of the entries into cells, since each cell can contain a different data type.
If you take the approach of changing the variable to cell then you can do that at the time you create the table, when you make the call to table() or cell2table(); if you did that then you could do the kind of looping you are doing, just making sure to do cell indexing instead of regular indexing.
If you take the approach of changing the variable to character vector, then you would need to replace the entire variable of the table at the same time, so your loop would have to write to a different location and then afterwards you would replace the entire variable with the computed new result.
... But you are not using a table. You are attempting to use a single numeric array. numeric arrays can never store characters. You can switch to using a table(), or you can switch to using a cell array. Perhaps you want code like this:
NumRows = 3;
MDL= ['AAA';'BBB';'CCC';'DDD'];
x = 1:4;
y = 101:104;
TABLE = num2cell([x;x;x]);
EX = [5;2;7;4];
Row = 1;
RowTurnedTable=TABLE(Row,:);
for i = 1:length(RowTurnedTable)
if RowTurnedTable{i} == EX(i)
TABLE{Row,i}=MDL(i, :);
end
end
  1 件のコメント
Matt Brianik
Matt Brianik 2018 年 1 月 15 日
this helps, but it is only working for the first row, how do you get it to repeat in rows 2 and 3

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by