X and Y must be same length, however they already are?

조회 수: 3 (최근 30일)
Deniz Mert Yayla
Deniz Mert Yayla 2019년 10월 19일
댓글: Deniz Mert Yayla 2019년 10월 19일
So I am trying to run the following code:
x = [];
y = [];
fid = fopen('HWPlotting.dat');
if fid == -1
disp('Error:Cannot open file')
else
total = 0;
while ~feof(fid)
str = fgetl(fid);
str = str(3:end);
[xstr, rest] = strtok(str, 'y');
y = [y, str2double(rest(3:end))];
total = total + 1;
end
closeresult = fclose(fid);
if closeresult == 0
disp('File closed')
end
end
area(x,y)
xlabel('x')
ylabel('y')
title([num2str(total),' data points'])
However I get the error two errors:
"Error using area (line 52)
X must be same length as Y.
Error in HWPlotting (line 25)
area(x,y)"
The .DAT file I use is simple, it's like this and nothing else:
x 0 y 1.1
x 1.3 y 2.2
x 2.2 y 6
x 3.4 y 7.4
So what's really going on? What in this code is not the same length?

채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 19일
You do not store any x values, so it is [] after the reading loop.
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 10월 19일
Needs to be
x = [x str2double(xstr)];
Deniz Mert Yayla
Deniz Mert Yayla 2019년 10월 19일
Oh.. You are right. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by