Problem of storing cell arrays into excel file

3 views (last 30 days)
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

Accepted Answer

Image Analyst
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
Cheeesepondue
Cheeesepondue on 3 Jan 2019
Edited: Cheeesepondue on 3 Jan 2019
@Image Analyst I finally figured what you have suggested here. I just had an “aha” moment. I am so happy! Thank you!
Cheeesepondue
Cheeesepondue on 3 Jan 2019
@Image Analyst
Although I’ve completed what I inteded to code, I am planning to add some more basic thingsm such as column headers and etc.
My Ideas is to concentanate cell arrays of headers, means and STDEVs with extracted raw data, and then use ‘xlsread’ syntax. I extracted name of files from each excel file by using
[~,txt] = xlsread(filename{1,k2});
the mean values and STDEVs are calculated by using
mean=mean(A,2);
std=std(A,2);
Do you think there is any better idea?
Best,

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!