フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by