Char Matrix to number

1 Ansicht (letzte 30 Tage)
Felipe
Felipe am 10 Sep. 2015
Kommentiert: dpb am 12 Sep. 2015
Hi I used this
ID = fopen(file.txt,'r');
for i=1:1000
bf = fgetl(ID);
if isempty(bf)
continue
end
if ~(buffer(1)==-1) && i>=2
for j=1:length(bf)
data(i,j)= bf(1,j);
end
end
end
And I get a NxM char Matrix with my data from IEEE. If i enter data matrix, matlab show[s] [th]e exact matrix. But when i try to get some number like data(1,3), all I get is nothing. How I can get my datas on this matrix in double ? Already tried srt2num.
Thanks
  2 Kommentare
dpb
dpb am 10 Sep. 2015
Bearbeitet: James Tursa am 10 Sep. 2015
Attach your data file (or paste a short representative section) so folks can see what might be the issue.
Otherwise, what's buffer above? It's undefined.
It appears the code above is simply
ID = fopen(file.txt,'r');
nSkip=2;
for i=1:nSkip,fgetl(ID);end % skip header lines
i=0;
while ~feof(ID)
bf = fgetl(ID);
if isempty(bf), continue, end
if bf(1)==-1, continue, end
i=i+1; % increment counter
data(i,:)= bf;
end
The above (as well as your code) relies on the length of the lines being fixed length; otherwise there will be a size mismatch in the character array storage.
Seems as though str2num should work on the above array if the values are properly formed but also seems like should be able to directly read the file with one of the formatted i/o functions such as textread, importdata or textscan but need specifics of the file format to say specifically how.
dpb
dpb am 12 Sep. 2015
"when i try to get some number like data(1,3), all I get is nothing."
Of course, that's not too surprising as you've saved the array simply as a character array and so data(1,3) is a single character, not a value. If there happen to be leading blanks in the field, then that's what you'll (not) see displayed; otherwise you'll get a single character numeral, punctuation (".+-") or perhaps an "E" or "e", etc., ...
Also, if you're in a locale that uses the comma instead of the dot for the decimal indicator, Matlab can't process it; you'll have to convert those to the period first. That just MIGHT be why str2num and friends failed. We can only go any further in actually specifically diagnosing the problems by seeing the actual data.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by