Export a cell array containing cell arrays
Show older comments
*** Using version 2018b. ***
I am trying to extract data from a large number of text files (~100). I want to extract the 5 rows that I need and create a csv / text / xlsx (not fussed) of all of them so that I can compare (see below).
However, my code so far gives me a 1*26 cell for A:Z, containing 26, 1*5 cells; which I can't do much with because I can't export an array of arrays (see screenshot).
*** UPDATE: ***
Image analyst has a solution, but it is very clunky for 26 columns. I can't get a for loop to work for this. https://uk.mathworks.com/matlabcentral/answers/119476-how-to-export-a-cell-array-into-excel-with-elements-of-different-sizes
My code below creates a 5 x 26 cell as required, but only populates the last column.
Many thanks in advance!
******************************************************************************************************************
for col = 1 : 26
theArray = output_matrix1{col};
xx = cell(5,26);
for row = 1 : size(theArray, 1)
xx{row,col} = theArray(row)
end
end


**************************************************************************************************************
My other issue, is that due to the requirements of the analysis function, the files are all labelled Results_A.txt, Results_B.txt, etc. I am able to loop through A:Z, but no luck with AA:AZ, etc. Is there a way around this or would it be easier to re-label the files 1:97?
*** UPDATE: *** Sulaymon fixed this issue. Thanks :D
QUICK FIX: 'Results_A%s.txt'; 'Results_B%s.txt'; 'Results_C%s.txt'
% % Sulaymon Eshkabilov, 2019
% https://uk.mathworks.com/matlabcentral/answers/473065-export-a-cell-array-containing-cell-arrays-with-letter-titles-a-z-aa-ab
for iteration = 'a':'z'
filename = sprintf('Results_%s.txt', iteration) %'Results_A%s.txt'; 'Results_B%s.txt'; 'Results_C%s.txt'
text = fileread(filename);
... % my code
end
*****************************************************************************************************************
3 Comments
Rik
on 26 Jul 2019
Please don't edit your question to update it. It is not clear if your question has meaningfully changed, so I can't tell if you have any issue that still has to be addressed. Please post a comment instead with a description of your remaining issues.
Image Analyst
on 26 Jul 2019
Why are you even using a cell array in the first place? I see no reason for it. You can do everything with regular, normal double arrays as usual, as far as I can tell. You can then use csvwrite(), dlmwrite(), writetable(), or xlswrite().
Hannah Bartlett
on 29 Jul 2019
Answers (3)
Sulaymon Eshkabilov
on 23 Jul 2019
0 votes
Hi,
An easy and quick fix is to re-label your data files with numbers 1:97 or whatsoever (as you have already noted) that works flawlessly.
1 Comment
Hannah Bartlett
on 24 Jul 2019
You need to generate the iteration labels (which is easiest with a cell array. I have once written a function that generates excel letter headers. If you have issues writing it yourself I can look it up for you.
Alternatively you can directly use the numeric labels with sprintf.
Edit to make it a bit easier to implement:
for iteration=27:53;
filename = sprintf('Results_%s.txt', getExcel(iteration));
7 Comments
Hannah Bartlett
on 24 Jul 2019
Rik
on 24 Jul 2019
The idea is to treat these letters as a numerical system. Binary has 2 values, decimal 10, so because we have 26 letters let's treat this as a kind of hexaduodecimal (if that exists as a word).
function out=getExcel(val)
%convert a scalar integer to an excel column ID
val=val-1;%map A to 0 and Z to 25
while val(end)>25
temp=val(end);
val(end)=mod(temp,26);
val(end+1)=(temp-val(end))/26
end
%now the array must be flipped and converted to a char array
out=char(val(end:-1:1))+'A';
end
(Written on mobile, untested code)
Rik
on 24 Jul 2019
Hi,
The proposed code by Rik is really good one, but there is one minor missing point in obtaining the variable out in a character array. The variable has to be:
out=char(char(val(end:-1:1))+'A');
Good luck
Sulaymon Eshkabilov
on 24 Jul 2019
Another point: you'd need lower cases - thus, 'a' instead of 'A'. The variable has to be:
out=char(char(val(end:-1:1))+'a');
Good luck
Rik
on 24 Jul 2019
Thank you for your suggestion. In that case the addition should just be moved.
out=char(val(end:-1:1)+'A');
Or maybe moving the char to the front will make the output inherit the char type:
out='A'+char(val(end:-1:1));%untested
Rik
on 24 Jul 2019
And about the upper and lower case: the text describes upper case, the code lower case. You can edit the function itself, or keep it congruent with excel and use the lower() function to convert the output to lower case.
Hannah Bartlett
on 24 Jul 2019
Sulaymon Eshkabilov
on 24 Jul 2019
Hi,
here is a rather simple solution:
OUT = char(97:122);
for ii = 1:26
Name=OUT(ii);
filename = sprintf('Results_a%s.txt', Name)
text = fileread(filename);
... % your code comes
... % your code comes
end
good luck
2 Comments
Rik
on 24 Jul 2019
This solution works well, but only for this specific case. Using more general code will allow using the same code for the single letter and dubble letter names.
I'm sorry if I'm coming across as grouchy. That is of course not my intention, but I am aware I might sound a bit grumpy.
Hannah Bartlett
on 25 Jul 2019
Edited: Hannah Bartlett
on 25 Jul 2019
Categories
Find more on Spreadsheets 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!