Horizontal concatenate in UItable table

4 次查看(过去 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))=[]

采纳的回答

Walter Roberson
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
Voss 2022-1-14
c = readcell('input.xlsx');
c(1,:) = [];
idx = ~cellfun(@ischar,c);
c(idx) = cellfun(@num2str,c(idx),'UniformOutput',false)
c = 22×6 cell array
{'bacon rashers' } {'250'} {'g' } {'738120'} {'Use By' } {'0' } {'black pepper' } {'20' } {'g' } {'738319'} {'Best Before' } {'10'} {'chicken breast'} {'200'} {'g' } {'738113'} {'Use By' } {'0' } {'chicken thigh' } {'150'} {''g'' } {'738114'} {''Use By'' } {'0' } {'egg' } {'12' } {''units''} {'738120'} {''Use By'' } {'0' } {'flour' } {'500'} {''g'' } {'738342'} {''Best Before''} {'3' } {''garlic'' } {'50' } {''g'' } {'738132'} {''Use By'' } {'0' } {''honey'' } {'200'} {''g'' } {'738369'} {''Best Before''} {'5' } {''ketchup'' } {'50' } {''g'' } {'738324'} {''Best Before''} {'5' } {''mayonnaise'' } {'80' } {''g'' } {'738142'} {''Best Before''} {'4' } {''milk'' } {'300'} {''ml'' } {'738231'} {''Best Before''} {'3' } {''mushroom'' } {'100'} {''g'' } {'738112'} {''Use By'' } {'0' } {''onion'' } {'250'} {''g'' } {'738116'} {''Best Before''} {'3' } {''parmesan'' } {'50' } {''g'' } {'738115'} {''Best Before''} {'6' } {''pasta'' } {'250'} {''g'' } {'738160'} {''Best Before''} {'3' } {''pork chop'' } {'300'} {''g'' } {'738115'} {''Use By'' } {'0' } {''potato'' } {'200'} {''g'' } {'738126'} {''Use By'' } {'0' } {''salmon'' } {'300'} {''g'' } {'738116'} {''Best Before''} {'4' } {''salt'' } {'100'} {''g'' } {'738669'} {''Best Before''} {'10'} {''shrimp'' } {'200'} {''g'' } {'738114'} {''Use By'' } {'0' } {''soy sauce'' } {'500'} {''ml'' } {'738627'} {''Best Before''} {'5' } {''tomato'' } {'40' } {''g'' } {'738118'} {''Use By'' } {'0' }
c_new = cell(size(c,1),1);
for i = 1:size(c,2)
c_new = strcat(c_new,c(:,i),{' '});
end
display(c_new);
c_new = 22×1 cell array
{'bacon rashers 250 g 738120 Use By 0 ' } {'black pepper 20 g 738319 Best Before 10 ' } {'chicken breast 200 g 738113 Use By 0 ' } {'chicken thigh 150 'g' 738114 'Use By' 0 ' } {'egg 12 'units' 738120 'Use By' 0 ' } {'flour 500 'g' 738342 'Best Before' 3 ' } {''garlic' 50 'g' 738132 'Use By' 0 ' } {''honey' 200 'g' 738369 'Best Before' 5 ' } {''ketchup' 50 'g' 738324 'Best Before' 5 ' } {''mayonnaise' 80 'g' 738142 'Best Before' 4 ' } {''milk' 300 'ml' 738231 'Best Before' 3 ' } {''mushroom' 100 'g' 738112 'Use By' 0 ' } {''onion' 250 'g' 738116 'Best Before' 3 ' } {''parmesan' 50 'g' 738115 'Best Before' 6 ' } {''pasta' 250 'g' 738160 'Best Before' 3 ' } {''pork chop' 300 'g' 738115 'Use By' 0 ' } {''potato' 200 'g' 738126 'Use By' 0 ' } {''salmon' 300 'g' 738116 'Best Before' 4 ' } {''salt' 100 'g' 738669 'Best Before' 10 ' } {''shrimp' 200 'g' 738114 'Use By' 0 ' } {''soy sauce' 500 'ml' 738627 'Best Before' 5 '} {''tomato' 40 'g' 738118 'Use By' 0 ' }

类别

Help CenterFile Exchange 中查找有关 Oceanography and Hydrology 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by