Filter löschen
Filter löschen

error when assign a matrix

2 Ansichten (letzte 30 Tage)
Fei
Fei am 19 Apr. 2013
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.

Antworten (2)

Jan
Jan am 19 Apr. 2013
Bearbeitet: Jan am 19 Apr. 2013
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 am 21 Apr. 2013
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 Kommentar
Walter Roberson
Walter Roberson am 21 Apr. 2013
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by