I like to save a cell array into text file

조회 수: 4 (최근 30일)
Majid Vaghari
Majid Vaghari 2021년 12월 27일
댓글: dpb 2021년 12월 27일
Hello to all,
I have a big cell array and I like to save it into text file.
the cell array contains two columns and each colum contain name of an element for example 'Al'.
a row of the cell array is like: ['Al' 'Mg']
now I'd like to save all of rows into a text file and save it like AlMg no space between Al and Mg in the text file.
for example if in the cell array is :
Al Mg
Na k
Al Sc
in the text file:
AlMg
NaK
AlSc
I attached the Untitled.m file
if you run the program you will get the Binary which is my cell array and I like to save it into a text file like I said above.
Note that the Binary cell array is big array!
best
Majid
  댓글 수: 1
Majid Vaghari
Majid Vaghari 2021년 12월 27일
I use MATLAB 2014b,
Please see the attachment.
best

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2021년 12월 27일
>> c={'Al','Mg';'Na','K'}
c =
2×2 cell array
{'Al'} {'Mg'}
{'Na'} {'K' }
>> join(c,'')
ans =
2×1 cell array
{'AlMg'}
{'NaK' }
>>
So, the solution to your problem is
>> writecell(join(c,''),'joined.txt')
See what we got...
>> type joined.txt
AlMg
NaK
>>
QED
  댓글 수: 3
dpb
dpb 2021년 12월 27일
If possible, upgrade; this is just one of the many advances since R2014. Well worth it if at all possible.
If not, then have to revert to low-level io -- still not too bad...but just a little more effort.
>> fid=fopen('joined.txt','w');
>> delete joined.txt % get rid of old file so we know we did something new
>> for i=1:size(c,1)
fprintf(fid,'%s\n',strcat(c{i,1},c{i,2}));
end
>> fid=fclose(fid);
and will show us the same result...
>> type joined.txt
AlMg
NaK
>>
dpb
dpb 2021년 12월 27일
Be better to put your follow-up comment as comment instead of Answer...
I would not expect any real difference in performance with the upgrade, just user convenience; it reverts to the same low-level io calls in the end, anyways.
You could see if it made any difference at all to do the concatenation first outside the loop, but I'd still expect that to be minimal. If your array is so big as to make the i/o time noticeable, then it must be quite sizable, indeed.
c=strcat(c(i,1),c(i,2)); % join cell array first
fid=fopen(...)
for i=1:size(c,1)
fprintf(fid,'%s\n',c{i,1});
end
or, alternatively, forget about the join entirely...just a little more code in the output is why I didn't before, didn't figure the time factor would be an issue...
...
fprintf(fid,'%s\n',[c{i,}]);
or
fprintf(fid,'%s%s\n',c{i,:});

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Majid Vaghari
Majid Vaghari 2021년 12월 27일
Thanks for your answer.
I'll update my MATLAB. I have done your older solution version but it takes time for matlab to solve all the cells into one file. So, I was trying to find something faster. So, I think I should upgrade my MATLAB.
beST

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by