필터 지우기
필터 지우기

Mis-type append problem

조회 수: 1 (최근 30일)
Ugur Sahin
Ugur Sahin 2021년 5월 9일
댓글: Jan 2021년 5월 10일
Hi guys,
I give an error when i try to append my values to arrays that ı create before while loop like that:
>> tryy
15.5023.758.0017.005.5019.0024.002.507.5011.0013.003.7525.009.7522.0018.006.0012.502.0021.50
2158.701678.152316.002061.302207.501708.301784.702575.002357.902256.702165.202399.551779.802336.751765.302053.502414.402200.502654.201753.70
Error using scatter (line 44)
Must supply X and Y data as first arguments.
Error in tryy (line 21)
scatter(x,y)
I think the problem involves w/ the my values type after when i append them to list because after i append them to list when i call as i.e x(4) they return to me 5 as an integer it has to return 5.50 as float .
This is propellant.csv
Thank You
fileID=fopen('propellant.csv');
y=[];
x=[];
counter=0;
while ~feof(fileID)
counter=counter +1;
tline=fgetl(fileID);
if counter > 1
newStr= split(tline,",",2);
a=newStr{2};
b=newStr{1};
x=[x a]; % I am tryin to append array these are my values as a to x and b to y
y=[y b];
end
end
disp(x);
disp(y);
scatter(x,y) % this is line 21

답변 (1개)

Jan
Jan 2021년 5월 9일
편집: Jan 2021년 5월 9일
The problem is, that you provide CHAR vectors, but scatter requires numerical arrays.
readtable would be smart, but this works also:
fileID = fopen('propellant.csv');
fgetl(fileID); % Skip header
data = fscanf(fileID, '%g,%g', [2, inf]);
fclose(fileID);
scatter(data(1, :), data(2, :));
  댓글 수: 2
Ugur Sahin
Ugur Sahin 2021년 5월 10일
x=[];
y=[];
fileID = fopen('propellant.csv');
fgetl(fileID); % Skip header
data = fscanf(fileID, '%g,%g', [2, inf]);
fclose(fileID);
for i=1:20
y=[y data(1,i)];
x=[x data(2,i)];
end
scatter(x,y)
When ı try your code values and scatter plot was different i don't know why after i have tried add to list my values separetly and it worked but i fscanf was very useful thank you :)
Jan
Jan 2021년 5월 10일
You just swapped x and y. Try:
scatter(data(2, :), data(1, :));

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by