Azzera filtri
Azzera filtri

How to convert following dataset to matrix

3 visualizzazioni (ultimi 30 giorni)
%%%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'
What I want is follows: 1. would like to remove ' ' 2. want to have these raw data in a matrix or array so that I can plot it from matlab
Any comments will be appreciated?
BR MJ

Risposta accettata

Matt J
Matt J il 1 Ott 2012
You can do this only for the numeric data. One way is as follows
numericMatrix=cellfun(@(c)str2double(strrep(c,',','.')), sampledata(:,[1 4 5 6]));

Più risposte (1)

Ryan G
Ryan G il 1 Ott 2012
It looks like you wrote a custom script to import this data. I would suggest using the import tool once, then generating a script/function once you've established how you want to import your data.
In the tool it's easy to choose which columns you want, what the delimiter is and what formate (matrix,vector,dataset etc). From there the generate script or function option would allow you to do the same thing to multiple files via MATLAB-script.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by