"vertcat" error with array of strings

11 ビュー (過去 30 日間)
Ksenia
Ksenia 2011 年 3 月 2 日
I have an array of strings, which is used to store a collection of filenames to retrieve data. Depending on the task at hand, sometimes I need to access the data from only certain files, in which case I would like to comment out some filenames from the array.
When I do that, I get an error saying:
Error using ==> vertcat
CAT arguments dimensions are not consistent.
Here's a test program that illustrates the problem:
fprintf('\n\n BEGIN \n\n');
input_file_array = {...
'test1.txt',...
'test2.txt', ...
%'test3.txt', ...
'test4.txt', ...
};
fprintf('\n\n DONE \n\n');
Note that I get the error only when there is a continuation of the array elements after the commented line (but if I were to comment out both test3 and test4, I get no error).
I'm using Matlab R2007a.

採用された回答

Walter Roberson
Walter Roberson 2011 年 3 月 2 日
You can't do that. The commented out line is treated as a blank line, which switches over the parsing to vertical concatenation, the same as if you were using { 'test1.txt', ... 'test2.txt' 'test4.txt' }
which would be equivalent to trying to do
{ 'test1.txt', 'test2.txt'; 'test4.txt'}
What I suggest is that you switch over to vertical concatenation anyhow. Transpose the resulting cell array if you need to:
input_file_array = {...
'test1.txt';...
'test2.txt'; ...
%'test3.txt'; ...
'test4.txt'; ...
};
The % will be treated as a blank line with the implied vertical concatenation, but since you are using vertical concatenation anyhow you don't run in to a conflict.
You can simplify this code as:
input_file_array = {
'test1.txt'
'test2.txt'
%'test3.txt'
'test4.txt'
};
taking advantage of the implicit vertical concatenation.
  1 件のコメント
Andrew Newell
Andrew Newell 2011 年 3 月 2 日
I like this better than my solution.

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

その他の回答 (2 件)

Andrew Newell
Andrew Newell 2011 年 3 月 2 日
That commented line is just like having an empty line in the middle of your command (try it!). You could do this:
input_file_array = {...
'test1.txt',...
'test2.txt', ... %'test3.txt', ...
'test4.txt', ...
};

Ksenia
Ksenia 2011 年 3 月 3 日
Thank you all very much for your quick and insightful replies! Such an easy explanation (and solution) for something that has been bugging me for a long time.
Appreciate you help!

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by