Export a cell array containing cell arrays

*** 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 个评论

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.
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().
Rik: I had two separate issues. One of them has been solved so I moved this section to the bottom to highlight the other question which I have left at the top.
QUESTION: How to get a 1*26 cell array containing 5*1 arrays into a 5*26 cell array using a for loop?
Image Analyst has a solution without the for loop which works, but I have too many columns for this method
---------------------------------
Image Analyst: My contents are a variety of strings read from a text file so a double array wouldn't work unless I can find a way to only read the numbers from the relevant rows in the text file.

请先登录,再进行评论。

回答(3 个)

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
Rik 2019-7-23
编辑:Rik 2019-7-24
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 个评论

Thanks, I had thought about that but haven't written anything yet because I still can't extract the cell array :(
Any thoughts on getting that working?
But, I would appreciate it if you could help with generating the excel letter headers as I don't want to have to rename all of my files manually!
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)
Comment posted as answer by Sulaymon Eshkabilov moved here:
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
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
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
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.
Great! Thank you. I'll give that a go first thing tomorrow and let you know how I get on :)

请先登录,再进行评论。

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 个评论

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.
That's perfect! I didn't think to just add the letter in front like:
'Results_A%s.txt'; 'Results_B%s.txt'; 'Results_C%s.txt'
*** You don't need the character bit though. ***
Actually you do! Otherwise [for iteration = 'a':'z'] adds and extra 96 columns at the start!
Thanks!!!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by