Mis-type append problem

1 view (last 30 days)
Ugur Sahin
Ugur Sahin on 9 May 2021
Commented: Jan on 10 May 2021
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

Answers (1)

Jan
Jan on 9 May 2021
Edited: Jan on 9 May 2021
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 Comments
Ugur Sahin
Ugur Sahin on 10 May 2021
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 on 10 May 2021
You just swapped x and y. Try:
scatter(data(2, :), data(1, :));

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!