Dimension error using VERTCAT, but pretty sure dimensions are consistent

7 次查看(过去 30 天)
rawdata is a numeric matrix (size 2500x136 double)
text is a string matrix (size 2x136)
Text matrix contains the labels I want to add to the rawdata matrix. I double-checked the variable dimensions by using the size( ) function. Help?!?
error message:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in openfile (line 25)
labeled_data=[text; rawdata];
code:
%read the text file
fileID=fopen('Master_inventory.txt');
inventory=textread('Master_inventory.txt', '%s' ...
, 'delimiter', '\n', 'whitespace', '');
% search for directory
c = cellstr(inventory);
filedesired = 'M29AA216afc01c1asfh_0000.3LA';
indx = regexpcell(c,filedesired);
%retrieve directory
pat = inventory(indx);
path = cellstr(pat);
%open file
raw=path{1};
rawdata=load(raw);
%add column labels
filename = 'DC_labels.xls';
[data, text] = xlsread(filename,'A1:EF2');
text = cellstr(text);
labeled_data=[text; rawdata];

采纳的回答

Kelly Kearney
Kelly Kearney 2014-7-23
When you try to concatenate the cell array ( text ) with a numeric array ( rawdata ), the numeric array is treated as a single cell, so you end up trying to concatenate a 2 x 136 cell array with a 1 x 1 cell array. Hence the error.
Instead:
labeled_data = [text; num2cell(rawdata)];
A few other tips
text = cellstr(text)
should be unnecessary, since xlsread already returns a cell array of strings. Also, using text as a variable name isn't a great idea, since it overshadows the text function (for plotting text).

更多回答(1 个)

dpb
dpb 2014-7-23
...rawdata is a numeric matrix...text is a string matrix...
Text matrix contains the labels I want to add to the rawdata matrix. ...
labeled_data=[text; rawdata];
Can't mix numeric and character data in a single array; can only use a cell array as a container for the two types as separate cells.
There's no need to have them both in the same array, anyway, what is your end objective?
There is the newer table object that does keep a set of labels with it, check the doc's for details on it. Perhaps that's what you're looking for.
  1 个评论
Zeke Merchant
Zeke Merchant 2014-7-23
Applying this code to a GUI. GUI inputs are data specs like subject ID, displacement, data type, and trial number. GUI searches inventory of data, retrieves data, adds column labels, and renames the data as 'dataname_edited'

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by