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 Center および File ExchangeLarge 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