Problem of storing cell arrays into excel file
3 views (last 30 days)
Show older comments
Cheeesepondue
on 30 Dec 2018
Commented: Cheeesepondue
on 3 Jan 2019
Hi everyone,
I very recently started using MATLAB for research purpose, and am learning lots of basic concepts.
I have an issues with exporting multiple data into an excel file.
Here, I am trying to export A {: , i} file. The reason why I used cell arrays instead of numeric arrays A (: , i) is that the latter does not work since the length of columns is different.

"Unable to perform assignment because the size of the left side is 234-by-1 and the size of the right side is 204-by-1."
I am able to export data by using xlswrite one at a time by using the following commands.
xlswrite(OutputFileName,A{1});
xlswrite(OutputFileName,A{2});
But I wall all the columns saved in the same sheet of an excel file.
Thank you.
Best,
XLfile = dir('*.csv'); %List contents of the current folder
[~, index] = sort(str2double(regexp({XLfile.name},'\d+','match','once')));
XLfile = XLfile(index);
% numeric digits in the cell array are extracted by regexp function.Sort numeric values.
% Find indecs, not values, by using [~, index]
filename = {XLfile.name}; %Create cell arrays of character vectors.
OutputFileName = 'Load_of_samples';
for i = 1:2;
for i = 1:2;
A{:,i} = xlsread(filename{1,i},1,'C:C');
end
xlswrite(OutputFileName,A{1,i});
end
0 Comments
Accepted Answer
Image Analyst
on 30 Dec 2018
However your for loop is not right. It is NOT GOOD to have a for loop with index called "i" inside another for loop with the index ALSO named "i". In fact "i" (the imaginary number) is not ever a good name AT ALL for an index. Use something like k1 for the outer loop index and k2 for the inned loop index. So let's fix that first.
Now, you need to give them different cell ranges. If you don't specify, it puts everything at cell A1. So when you did
xlswrite(OutputFileName,A{1});
xlswrite(OutputFileName,A{2});
A{2} blasted over the top of A{1}, well almost all of it except for the last 30 rows of it from row 205 to 234. Use something like
cellReference = sprintf('%c1', Excel2Col(i)); % Row 1 of different column letters.
xlswrite(OutputFileName,A{1,i}, 'Sheet1', cellReference);
You should be able to find something in the File Exchange that converts a column number to a letter code that Excel uses. I think there are a number of them called things like Excel2Col().
finally, your xlswrite() is not going to write out both columns of the cell array because you indexed it as "i". What you need to do is write both out explicitly:
cellReference = sprintf('%c1', Excel2Col(i)); % Row 1 of different column letters.
xlswrite(OutputFileName,A{1,1}, 'Sheet1', cellReference);
cellReference = sprintf('%c1', Excel2Col(i+1)); % Row 1 of different column letters.
xlswrite(OutputFileName,A{1,2}, 'Sheet1', cellReference);
That will write them side by side in adjacent columns.
OR you could just preallocate A in advance to the known number of rows and assign only the proper length to the proper column.
Attach two of your CSV files if you still have trouble.
4 Comments
More Answers (0)
See Also
Categories
Find more on Data Import from MATLAB 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!