Export a cell array containing cell arrays

6 views (last 30 days)
*** 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
Image Analyst
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
Hannah Bartlett on 29 Jul 2019
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.

Sign in to comment.

Answers (3)

Sulaymon Eshkabilov
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
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
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
Hannah Bartlett on 24 Jul 2019
Great! Thank you. I'll give that a go first thing tomorrow and let you know how I get on :)

Sign in to comment.


Sulaymon Eshkabilov
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
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
Hannah Bartlett on 25 Jul 2019
Edited: Hannah Bartlett on 25 Jul 2019
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!!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!