Replacing numbers in dataset by string
3 次查看(过去 30 天)
显示 更早的评论
Hello, could you help me please with this code:
for i=1:length(inoperationDatatree1.SystemSlices)
if (inoperationDatatree1.SystemSlices(i)>0 && inoperationDatatree1.SystemSlices(i)<=50)
inoperationDatatree1.SystemSlices(i)={'<=50'};
elseif (inoperationDatatree1.SystemSlices(i)>51 && inoperationDatatree1.SystemSlices(i)<=100)
inoperationDatatree1.SystemSlices(i)={'50...100'};
elseif (inoperationDatatree1.SystemSlices(i)>101 && inoperationDatatree1.SystemSlices(i)<=150)
inoperationDatatree1.SystemSlices(i)={'101...150'};
else inoperationDatatree1.SystemSlices(i)={'>150'};
end
end
I need to replace each of row by interval...
2 个评论
David Young
2016-1-30
Storing those strings is an unusual thing to want to do. Can you explain what the purpose is, because there might be a much better way to solve the underlying problem.
采纳的回答
John BG
2016-1-30
Quoting from Mathworks documentary: http://uk.mathworks.com/help/stats/dataset-class.html?s_tid=srchtitle
.. The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information ..
you seem to be attempting to cast a string in the place that you initially allocated to a number. If you had a cell, that would be fine, you can even replace numbers with entire arrays, or nesting cells inside cell positions.
why don't you translate your dataset into a table and start replacing table elements from there?
The Mathworks documentary starter example:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
pull some table elements with
T(1,1)
ans =
Age
_____
Smith 38.00
or
T(3,4)
T(3,4)
ans =
BloodPressure
________________
Williams 125.00 83.00
Yet T(1,1)='38' does not work either
but if you convert your table to a cell
Tcell=table2cell(T)
Now you can modify any field of the cell, you can have numbers next to strings in neighbouring cell fields.
Now mind the gap:
Tcell_1_1=Tcell(1,1)
Tcell_1_1 is still a cell
if you try Tcell(1,1)='38' doesn't work but the following works:
Tcell{1,1}='38'
Check
Tcell
and you see all Tcell fields containing cells but the (1,1) field where you just cast in a string.
Hope it helps, if so please click on thumbs up
John
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!