VERTCAT error when trying to populate array

Hi folks,
I am trying to write some fata into an array, which I will then write to excel. I get the VERTCAT error when trying to create the array (Error using vertcat Dimensions of arrays being concatenated are not consistent).
Can you help identify why this is please?
Each column of data should be 256 elements long.
My code is as follows:
headings = {'Pixel Value', 'R', 'G', 'B', 'Grey'};
pixelValue = (0:255)';
allCoke_R = imhist(allCokeMask(:,:,1));
allCoke_G = imhist(allCokeMask(:,:,2));
allCoke_B = imhist(allCokeMask(:,:,3));
allCokeGrey = imhist(rgb2gray(allCokeMask));
dataCollectionAllCoke = zeros(257, 5);
dataCollectionAllCoke = [headings; pixelValue, allCoke_R, allCoke_G, allCoke_B, allCokeGrey];
writecell(dataCollectionAllCoke, resultsPath, 'Sheet', nameStringExcel, "Range", 'A1');

 採用された回答

Jonas
Jonas 2021 年 7 月 3 日

1 投票

try putting {} around the second line of concatenation
dataCollectionAllCoke = [headings; {pixelValue, allCoke_R, allCoke_G, allCoke_B, allCokeGrey}];
or convert all variable entries to cells
dataCollectionAllCoke = [headings;num2cell([pixelValue, allCoke_R, allCoke_G, allCoke_B, allCokeGrey])];
both should work concerning the concatenation, but i am not sure about the needed format for the writematrix() command

4 件のコメント

Teshan Rezel
Teshan Rezel 2021 年 7 月 4 日
Hi, thanks for this! It seems to have gotten rid of the error. However, I am now getting the following output in the excel file, instead of 5 columns of 256 numbers each!
Any ideas on how to fix this please/
Jonas
Jonas 2021 年 7 月 4 日
did you use the second solution? or the first one? i think the second is more appropriate as input for writecell()
if you already used the second solution, can you provide the variable allCokeMask ?
Jonas
Jonas 2021 年 7 月 6 日
i tested the code myself now, it works as expected. of course i don't know what you spcified in the further parameters of the writecell() call
headings = {'Pixel Value', 'R', 'G', 'B', 'Grey'};
pixelValue = (0:255)';
allCokeMask=randi(255,100,100,3);
allCoke_R = imhist(allCokeMask(:,:,1));
allCoke_G = imhist(allCokeMask(:,:,2));
allCoke_B = imhist(allCokeMask(:,:,3));
allCokeGrey = imhist(rgb2gray(allCokeMask));
dataCollectionAllCoke = [headings; num2cell([pixelValue, allCoke_R, allCoke_G, allCoke_B, allCokeGrey])];
writecell(dataCollectionAllCoke, 'test.xls');
Teshan Rezel
Teshan Rezel 2021 年 7 月 7 日
@Jonas this works, thank you!

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by