1
votes

I had to update MATLAB recently from 2008 to 2014.

MATLAB's importdata no longer outputs just an array of useable values if there's any non-number text in the file. Testing shows that if I remove all my comments from my file, importdata returns the required data.

I tried something like this

structure = importdata('filename.txt')
structure.data

but my first line, which has a comment at the end of line (and thus non-number text) gets cut off. I have a bunch of comments throughout my data files and I'd rather not have to remove all my comments.

This answer seems out of date.

Is textscan the only way to fix this?

Data file I've been working with.

% Vin: 5 MHz 6.5 mV pk-pk
% ADRF: Pre: 6 dB, Filt: 31 MHz, VGA: 28 dB, Post: 12 dB
% VGain Vin     Vout
0   6.51    4.55 % Dirty input
40  6.52    4.57
70  6.54    4.60
110 6.55    4.88
160 6.54    6.21
200 6.53    7.83
240 6.54    10.36
270 6.53    12.95
320 6.53    18.10
360 6.52    24.70
400 6.52    32.20
440 6.51    44.60
480 6.51    57.90
520 6.52    79.50
560 6.51    105.3
600 6.53    147.9
640 6.54    195
680 6.53    272
720 6.51    357
760 6.50    500
800 6.50    677
840 6.47    881
880 6.47    993
920 6.47    1012
960 6.47    1012
1000    6.47    1012
1
@Divakar I'd expect it to work similar to importdata of 2008. For this file it'd return a 25x3 double the first line of which would be 0 6.51 4.55. However structure.data(1,:) currently returns 40.0000 6.5200 4.5700 - David
Can you ever have more than one % in a line? - Divakar
Possibly? I'd expect it to just ignore the rest of the line including the second % - David
if you insist on your dirty inputs, textscan is the easiest way to go. Or you do some batch processing in advance. - Robert Seifert
Also, since importdata is not a built-in function, the code is easily accessible. You can just copy the file 'importdata.m' from your old Matlab 2008 and copy it in your new Matlab path (would be wise to change the name though). Then use this old version instead of the latest one. - Hoki

1 Answers

2
votes

Is there any reason why you don't want to use textscan? This works for me in Matlab 2010 and 2013:

fid=fopen('testdata.dat');
data=textscan(fid,'%f %f %f','Headerlines',3,'Commentstyle','%');
fclose(fid);
data=cell2mat(data);

EDIT: As long as you don't have a comment in the first line of your data, importdata('testdata.dat') should work fine. There seems to be a change in the way the number of headerlines is determined between the Matlab versions you are comparing. If you prefer importdata to textscan, try this:

data=importdata('testdata.dat',' ',3)

then data.data should contain all your data and it is still quite readable.