error when assign a matrix

조회 수: 1 (최근 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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by