0

I want to read a row with numeric data from a file which contains one text line and few lines with numeric data. I have tried this by using fopen and textscan(as all string value) command in MATLAB. After loading all the data and trying to do any mathematical operation it is showing the following error.

Undefined function or method 'plus' for input arguments of type 'cell'.

I'm trying to loading the following file which contains following data:

K V M UV JV CI SI JRM MRJ MIM JIJ VB UB
90000 0 0 0 0 0 0 0 10800 216000 205200 226800 205200 431940 215970 215970

165026 122 122 99 23 105 7 27 10811 215874 275166 226800 205200 431940 215970 215970

165027 132 122 49 23 115 9 97 10911 215674 275166 226800 205200 431940 215970 215970

165028 142 122 79 23 155 7 107 10711 215774 225166 226800 205200 431940 215970 215970

I require only the numerical data of 3rd row for my use.Please help me.

Thank you for your help in this regards. Deepak

1 Answers1

0

You've got a couple things going on here, and without seeing your code, it's hard to know exactly what the problem is.

When you load the data in as a string, it's exactly that, a string, so operators like plus aren't going to get you very far. textscan also returns a cell array, which is fine for holding different types of data in a single variable, but not so great for doing math on.

As long as your file isn't ridiculously long, and you know the format, you could parse the file line-by-line with fgetl.

m = 1;
fid = fopen(fileName);
headerLine = fgetl(fid);% read in the first line, then ignore it
dataLine = fgetl(fid); 
while ischar(fileLine) % EOF will return -1
    C = textscan(dataLine,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','delimiter',char(32));
    firstVariable(m) = C{1};
    secondVariable(m) = C{2}; % etc.
    blankLine = fgetl(fid); %this line is blank
    dataLine = fgetl(fid); %this line has data, read it in and finish the loop 
    m = m +1;
end 
fclose(fid)

If you know (or can otherwise calculate) the number of lines you will have beforehand, you can and should preallocate your variables, or things will slow way down. If you have a good idea on an upper limit, you can allocate more space than you'll need and then trim them down at the end using the value of m.

craigim
  • 3,884
  • 1
  • 22
  • 42