Export a cell array containing cell arrays
5 次查看(过去 30 天)
显示 更早的评论
*** 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 个评论
Image Analyst
2019-7-26
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().
回答(3 个)
Sulaymon Eshkabilov
2019-7-23
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
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 个评论
Rik
2019-7-24
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
2019-7-24
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 个评论
Rik
2019-7-24
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!