0

I'm trying to write a script using MATLAB that reads from a txt file (which has 100*3 elements written in a single column). I want to read them 100 elements at a time and apply a fit exponential function. This is what I wrote:

defaultPath = 'my/default/path/';    
prompt = 'file name? ';    
fileName = input(prompt,'s');    
fullPath = strcat(defaultPath,fileName);    
fileID = fopen(fullPath);    
for h = 1:3
    buff = textscan(fileID, '%d', 100);
    y=buff';
    x = zeros([100, 1]);
    for j = 1:100
        x(j,1) = j;
    end
    f = fit(x,y,'exp1');
    plot(f,x,y);
end

but it gives me this error :

X and Y must have the same number of rows.
NKN
  • 6,482
  • 6
  • 36
  • 55
  • [textscan](http://www.mathworks.com/help/matlab/ref/textscan.html) returns a cell array, so you probably need `buff{1}` to access a vector. And that only applies if the read was successful, you should check that. And you can construct `x` as `x=1:100;`. And you should `fclose` the file in the end. Are you familiar with matlab? Anyway, your specific error might come from `fit` not liking that one input is a row vector while the other is a column. – Andras Deak -- Слава Україні Sep 20 '15 at 10:39
  • @AndrasDeak no, is the first time that I use this language. I'll try what you have suggested me! – user5355783 Sep 20 '15 at 10:42
  • I edited my comment regarding the `plot`: I didn't realize that this was a special (overloaded) and legal syntax for `fitobject`s. – Andras Deak -- Слава Україні Sep 20 '15 at 10:45
  • If you are a first-time MATLAB user, it's good practise to start by [not overwriting build-in functions](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) like `i` and `j`. – Adriaan Sep 20 '15 at 10:47

2 Answers2

1

Your main problem is probably that the two input vectors to fit are shaped differently: one is of size [100 1], the other [1 100], i.e. one is a column vector and the other's a row. I suggest this:

defaultPath = 'my/default/path/';

prompt = 'file name? ';

fileName = input(prompt,'s');

fullPath = strcat(defaultPath,fileName);

fileID = fopen(fullPath);

for h = 1:3
    buff = textscan(fileID, '%d', 100);
    y=buff{1}';
    x = 1:length(y);
    f = fit(x,y,'exp1');
    figure; %open new window for plotting each slice of the data
    plot(f,x,y);
end

fclose(fileID);

Note that I added a figure call before the plot, so that the 3 sets of data are plotted on separate figures (otherwise the default behaviour would be that each plot overwrites the previous one in the same figure.

I also changed the definition of x such that it explicitly matches the length of y, this will prevent some errors in case there were problems with the read and y has nontrivial length. Anyway, it's best to avoid magic numbers and define everything in terms of others, whenever possible.

  • You are using `plot()` with *three* input arguments, which will throw you an error unless `y`, and thus `buff{1}.'` contains a `LineSpec` argument. For plotting 3D functions (i.e. `f(x,y)` vs `x` and `y`) use `plot3` – Adriaan Sep 20 '15 at 10:51
  • @Adriaan, exactly what I thought. But `fitobject`s override this function, see the first example in [the doc](http://www.mathworks.com/help/curvefit/fit.html), the [doc for the overloaded function](http://www.mathworks.com/help/curvefit/plot.html), and also [my corresponding comment](http://stackoverflow.com/questions/32678348/read-n-interegr-from-txt-file-matlab/32678471#comment53201620_32678348) above. – Andras Deak -- Слава Україні Sep 20 '15 at 10:53
  • Ow, urgh, that's not fair :(. This plots two things against the same `x` axis or what? I do not approve of this syntax... But point taken, there's your chance to catch up to me again ;) – Adriaan Sep 20 '15 at 10:57
  • 1
    @Adriaan, if you're new to matlab, consider reading the manual I just linked to:P It easyplots a smooth fitting function along with the `x` and `y` data which are estimated. – Andras Deak -- Слава Україні Sep 20 '15 at 10:59
  • Yea, I did read it, but they could change the name from `plot` to `plotcfit` or something, as to make it distinguishable for the default `plot` – Adriaan Sep 20 '15 at 11:00
  • 1
    @Adriaan, yeah, I agree. Polymorphism *is* a thing, but it's really confusing to allow clearly incompatible syntax in an overloaded function. – Andras Deak -- Слава Україні Sep 20 '15 at 11:02
0

You can use csvread as follows:

f = csvread(STRING_OF_FILE_NAME);

f will be a matrix containing the data.

Odedy
  • 80
  • 2
  • 8