Character in cell array, read and write

Hello all,
I have a data in cell something like this:
1 BCS ACK/NACK 2054 0.00000 10.0000 Inside
[Note: actually I have 100 lines like above]
Now what I want to do is break this into different colum and save to excel sheet so that I can creat some graphs. Can anyone suggest how to, pls?
BR MJ

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 17일
what is the size of your cell array
MJ
MJ 2012년 9월 26일
Hello Azzi,
Your code has been helpful, thanks a lot:-).
BR MJ

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 17일

0 개 추천

A={'1 BCS ACK/NACK 2054 0.00000 10.0000';'1 BCS ACK/NACK 2054 0.00000 10.0000'}
res=cellfun(@(x) regexp(x,' ','split'),A,'uni',false)
out=[]
for k=1:numel(res)
out=[out;res{k}]
end
xlswrite('file.xls',out)

추가 답변 (3개)

MJ
MJ 2012년 9월 18일

0 개 추천

Hello Azzi,
Thank you for your answer.
The problem is I have space bewteen followings:
1 BCS ->1 BCS BCS ACK/NACK ->BCS ACK/NCK
and so on.
The thing is the data is in char format as these are copied from .txt files. Now when I want to save it on XLS the spacing creating a huge problem showing matlab error. May I ask, how can I remove this spacing?
/MJ

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
Is there a problem with the code?
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
편집: Azzi Abdelmalek 2012년 9월 26일
In case you have more then one blanck
A={'1 BCS ACK/NACK 2054 0.00000 10.0000';'1 BCS AK/NCK 2054 0.00 10.0000'}
res=cellfun(@(x) regexp(x,' ','split'),A,'uni',false)
out=[]
for k=1:numel(res)
q=res{k};
q(cellfun(@(x) length(x)==0,q))=[]
out=[out;q]
end
xlswrite('file.xls',out)

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

MJ
MJ 2012년 9월 28일

0 개 추천

Hello Azzi,
I have a question as shown below:
Code starts here: files=[]; out=[];
for k=1:5 %Reading files: file1.txt through file5.txt textFilename=['File' num2Str(k) '.txt']; % opening each files FiletoOpen=fopen(textFilename,'r'); % all files-just for information files=[files; textFilename]; % all files-just for information % Data{k}= textread(textFilename, '%s', 'delimiter', '\n'); %
% pattern: \s: Whitespace, +: match one or more occurences
pat = '\s+';
res=cellfun(@(x) regexp(x,pat,'split'),Data{k},'uni',false)
for m=1:numel(res)
q=res{m};
q(cellfun(@(x) length(x)==0,q))=[]
out=[out; q]
end
end
I get something like this: '1' 'BCS' 'ACK/NACK' '2054' '0' '10' 'Inside' '2' 'BCS' 'ACK/NACK' '30107' '0,1395' '10' 'Inside' '3' 'BCS' 'ACK/NACK' '30119' '0,16933' '10' 'Inside'
[Note: this is OK when I transfer datas from Matlb to Excel-I can plot raw datas]
I would like to have following instead above so that I can plot from Matlab directly:
1 BCS ACK/NACK 2054 0 10 Inside
I am not sure what is going wrong in my code-any advice will be appreciated.
Regards
/MJ

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 28일
편집: Azzi Abdelmalek 2012년 9월 28일
format your code
can you post samples of your data?
MJ
MJ 2012년 10월 1일
I have just added codes and raw datas below.

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

MJ
MJ 2012년 10월 1일

0 개 추천

%%%code starts from here:
files=[];
out=[];
for k=1:5
textFilename=['File' num2Str(k) '.txt'];
FiletoOpen=fopen(textFilename,'r');
files=[files; textFilename];
Data{k}= textread(textFilename, '%s', 'delimiter', '\n'); %
pat = '\s+';
res=cellfun(@(x) regexp(x,pat,'split'),Data{k},'uni',false)
for m=1:numel(res)
q=res{m};
q(cellfun(@(x) length(x)==0,q))=[]
out=[out; q]
end
end
Sample of Data:
'1' 'BCS' 'ACK/NACK' '2054' '0' '10' 'Inside'
'2' 'BCS' 'ACK/NACK' '30107' '0,1395' '10' 'Inside'
'3' 'BCS' 'ACK/NACK' '30119' '0,16933' '10' 'Inside'
'4' 'BCS' 'ACK/NACK' '30132' '3,23908' '10' 'Inside'
'5' 'BCS' 'ACK/NACK' '30134' '2,96011' '10' 'Inside'
'6' 'BCS' 'ACK/NACK' '2073' '0' '10' 'Inside'
'7' 'BCS' 'ACK/NACK' '30136' '2,29958' '10' 'Inside'
'8' 'BCS' 'ACK/NACK' '30141' '2,18639' '10' 'Inside'
'9' 'BCS' 'ACK/NACK' '2054' '0' '10' 'Inside'
'10' 'BCS' 'ACK/NACK' '30118' '0,16269' '10' 'Inside'
'11' 'BCS' 'ACK/NACK' '30118' '0,15273' '10' 'Inside'
'12' 'BCS' 'ACK/NACK' '30135' '3,33499' '10' 'Inside'
'13' 'BCS' 'ACK/NACK' '30142' '2,71714' '10' 'Inside'
'14' 'BCS' 'ACK/NACK' '2073' '0' '10' 'Inside'
'15' 'BCS' 'ACK/NACK' '30129' '2,10428' '10' 'Inside'

댓글 수: 1

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 1일
your data are well imported, what do you need to do

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

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

질문:

MJ
2012년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by