How can I use tblwrite in Matlab
1 view (last 30 days)
Show older comments
I have data which are as the following :
rownames11{kk}=char(rownames1(1,j))----1x225
colnames11={'K_minus_t';'K_minus_d';'a';'b';'c';'Err'}----1X6
values11{kk}}=[K_minus_t(j) K_minus_d(l) c resnorm]------1x225 ( variabe C contain three Values)
end
end
tblwrite(values11,colnames11,rownames11,'functionfitting.txt')
I tried to put these data as table type txt files by using tblwrite when I implement the code I receive this massage:
Error using tblwrite (line 64) Number of case names must equal number of data rows.
Error in lamm (line 96) tblwrite(values11,colnames11,rownames11,'functionfitting.txt');
Any help would be greatly appreciated. I will be grateful to you.
4 Comments
Geoff Hayes
on 18 Sep 2014
Ahmed - is the above code (in your question) correct, since it seems to imply that the number of rows is 225 and the number of columns is 6?
Regardless, try creating Values11 to a numeric matrix with 6 rows and 225 columns, rather than using a cell array
Values11 = zeros(6,225); % pre-allocate memory outside of for loops
for … %
% do stuff
rownames11{kk}=char(rownames1(1,j));
colnames11={'K_minus_t';'K_minus_d';'a';'b';'c';'Err'};
values11(:,kk)=[K_minus_t(j) K_minus_d(l) c resnorm]';
end
Note how the last line (in the above) sets the kkth column to a 6x1 vector (I added the apostrophe to transpose this to a column vector).
The you can call tblwrite but make sure that the second and third inputs are correct - since Values11 is 6x225, then the second input must have 225 strings, and the third input have six strings.
Accepted Answer
Geoff Hayes
on 19 Sep 2014
Summary of above discussion - the error message Number of case names must equal number of data rows is indicating that there is a discrepancy between the number of elements in rownames11 and the number of rows in data. Just prior to calling tblwrite, the following was typed in the Command Window
size(values11)
size(colnames11)
size(rownames11)
to determine the size of each cell array. As Ahmed posted, the sizes were: Values11 is 1x225 but it is cell contain 6 values Rownames is 1x6 colnames is 1x225.
The solution was to set Values11 to a numeric matrix with 6 rows and 225 columns, rather than using a cell array
Values11 = zeros(6,225); % pre-allocate memory outside of for loops
for … %
% do stuff
rownames11{kk}=char(rownames1(1,j));
colnames11={'K_minus_t';'K_minus_d';'a';'b';'c';'Err'};
values11(:,kk)=[K_minus_t(j) K_minus_d(l) c resnorm]';
end
Note how the last line (in the above) sets the kkth column to a 6x1 vector (the apostrophe was added to transpose this to a column vector).
tblwrite can then be called but must make sure that the second and third inputs are correct - since Values11 is 6x225, then the second input must have 225 strings, and the third input have six strings.
0 Comments
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!