creating array of fix and changing variables for xlswrite export
Show older comments
Hello All, I am in need of exporting my calculations to an excel spreadsheet. As shown in here: https://in.mathworks.com/matlabcentral/answers/101309-how-do-i-use-xlswrite-to-add-row-and-column-labels-to-my-matlab-matrix-when-i-write-it-to-excel-in-m
I have created 2 commands:
col_header={'Col1','Col2','Col3','Col4','Col5'};
data{ii}=[X1,X2,X3,X4,X5];
data_cells{ii}=num2cell(data{ii});
The variables X1,X2,X3,X4,X5 changes over the loop and I want to export each loop values for the same.
My main intent is to get a output file of excel spreadsheet with 5 columns named as:
col_header={'Col1','Col2','Col3','Col4','Col5'};
and the values of X1,X2,X3,X4,X5 will be pasted over the loop below the above titles respectively.
I tried this:
output_matrix=[{' '} col_header; data_cells];
But getting error of dimensions.
Dimensions of matrices being concatenated are not consistent.
Please help me.
7 Comments
alice
on 21 Jun 2017
I guess you want to use the code provided in https://in.mathworks.com/matlabcentral/answers/101309-how-do-i-use-xlswrite-to-add-row-and-column-labels-to-my-matlab-matrix-when-i-write-it-to-excel-in-m.
You can store your data in an array rather than a cell. Before the loop, you can preallocate data if you know the number of steps of your loop:
data = zeros(numberSteps,5);
During the loop, you write in this array:
data(ii,:)=[X1,X2,X3,X4,X5];
Then, after the loop, you can get a cell from this array this way:
data_cells = num2cell(data);
You have what you wanted, and you can add your headers and write to the excel file the same way as in the link:
output_matrix=[col_header; data_cells];
xlswrite('My_file.xls',output_matrix);
adi kul
on 21 Jun 2017
So your X1 and X2 are vectors with, let's say n elements.
- Are X3, X4 and X5 also vectors with n elements? Then you don't need a loop and you can directly use:
data=[X1,X2,X3,X4,X5];
- Or are they a number that changes in the loop? Then you keep the loop but in each line you just put the current element of X1 and X2:
data(ii,:)=[X1(ii),X2(ii),X3,X4,X5];
adi kul
on 21 Jun 2017
Can you tell what Matlab returns when you ask for the sizes of all items after you get the error (using the size function) please ? It would help understand.
nd = size(data(ii,:))
n1 = size(X1)
n2 = size(X2)
...
n5 = size(X5)
Answers (1)
>> [{' '} col_header]
ans =
' ' 'Col1' 'Col2' 'Col3' 'Col4' 'Col5'
>>
You've added a blank column to the first row that isn't in the second; hence you can't (vertically) concatenate the two. Just lose the blank entry and set the target row/column in the spreadsheet to 'B1' instead of 'A1' if that's the intent.
However, you don't really need to do the concatenation, write
output_matrix={'Col1','Col2','Col3','Col4','Col5'};
...
for ii=iLo:iHi % begin the loop
....
% compute the values here
...
output_matrix{ii+1}=num2cell([X1,X2,X3,X4,X5]);
...
end
Stylistic Comment: In general, having sequentially-named variables such as your X n above is indicative that haven't used the power of Matlab as fully as could have done. An array X may be much easier to code and maintain code for instead. We haven't seen the rest of the code that generated these names so can't give specifics, but it's worth looking at ways to vectorize the code and remove such repetition.
Categories
Find more on Matrices and Arrays 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!