Horizontal concatenate in UItable table
2 次查看(过去 30 天)
显示 更早的评论
Hello, I have a code that uploads excel data into UItable. I want to concatenate the rows of the table however, some of the output are special charaters. Please how do i get it right. Below is my code and the results.
Table Data
Output Data
This is my code
oldData = get(GUI.RetailerStockList,'Data')
for i=1:size(oldData,1)
oldData(i)={horzcat(oldData{i,:})};
end
oldData(:,2:size(oldData,2))=[]
0 个评论
采纳的回答
Walter Roberson
2022-1-13
oldData(i)={horzcat(oldData{i,:})};
Let us look at what that is being asked to do for i == 1:
oldData(1,:) = {'bacon rashers', 250, 'g', 738120, 'Use By', 0}
You horzcat() those, so you are asking for
['bacon rashers', 250, 'g', 738120, 'Use By', 0]
What is the rule when concatenating character vectors and numeric values?
TYPE | character | integer | single | double | logical
character | character | character | character | character | invalid
So when you [] together a character vector and a numeric value, the result is a character vector. This is not accomplished by formatting the number as text: this is accomplished by taking char() of the numeric value, looking up the underlying character codes as characters .
So the above line is equivalent to
['bacon rashers', char(250), 'g', char(738120), 'Use By', char(0)]
You should consider switching:
C = {"bacon rashers", 250, "g", 738120, "Use By", 0}
strjoin(string(C), ' ')
更多回答(1 个)
Voss
2022-1-14
c = readcell('input.xlsx');
c(1,:) = [];
idx = ~cellfun(@ischar,c);
c(idx) = cellfun(@num2str,c(idx),'UniformOutput',false)
c_new = cell(size(c,1),1);
for i = 1:size(c,2)
c_new = strcat(c_new,c(:,i),{' '});
end
display(c_new);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Oceanography and Hydrology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!