フィルターのクリア

error when assign a matrix

2 ビュー (過去 30 日間)
Fei
Fei 2013 年 4 月 19 日
Hi there,
I have four data files with a name 4.dat, 5.dat, 6.dat and 7.dat respectively in a folder. After treating these data by codes, I wish to assign the data name and computed result to a nX2 matrix named "result". Here is my code:
cd('C:\fitting\test\');
Allname=struct2cell(dir);
[m,n]=size(Allname);
for i=3:n
iname=Allname{1,i};
... ... %treatment of data, the interesting result is named "a"
result=zeros(n,2)
result(i,1:2)=[iname, a]
end
An error of "??? Subscripted assignment dimension mismatch" appears and the output is
result =
0 0
0 0
0 0
0 0
0 0
0 0
0 0
I don't know why this can happen.
Many thanks.

回答 (2 件)

Jan
Jan 2013 年 4 月 19 日
編集済み: Jan 2013 年 4 月 19 日
The relevant information is missing in your question: In which line does the error appear? What is the type and value of a and iname?
I guess, that the problem is in the line:
result(i,1:2)=[iname, a]
If iname and a are not scalars, you cannot write them to the [1 x 2] vector on the left hand side of this assignment. Perhaps you want result to be a cell array, when you want to mix the string(!) iname and the variable a.

Fei
Fei 2013 年 4 月 21 日
Thanks Jan.
a is a scalar;
iname is from
iname=Allname{1,i};
and
Allname=struct2cell(dir);
which is a cell array.
What shall I do if I want to output iname and a in the format of
4.dat a4
5.dat a5
6.dat a6
7.dat a7
where j.dat and aj stands for iname and a respectively for a particular data?
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 4 月 21 日
Initialize
result = cell(n, 2);
then in the loop,
result(i,1:2) = {iname, a};
assuming you want a cell array whose first element in a row is the file name and whose second element is the numeric value as a numeric value.
You could change it to num2str(a) if you want the second column to be a represented as a string, but still want columns.
If what you want is one row of characters, then initialize
result = cell(n,1);
and then in the loop,
result{i} = [inname, ' ', num2str(a)];
and after the loop,
result = char(result);

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

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by