Export a cell array containing cell arrays
3 views (last 30 days)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230979/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230980/image.jpeg)
**************************************************************************************************************
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
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().
Answers (3)
Sulaymon Eshkabilov
on 23 Jul 2019
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.
Rik
on 23 Jul 2019
Edited: Rik
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
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.
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.
See Also
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!