I have files, where the first column represent as x axis and the other columns are for y axis. I can get different XY curves by plotting them. For example, by plotting the first column with the second column I get one graph, again by plotting the first column with the third column I get another curve and so on. I need to calculated the slopes of each curves. Until now I have the programming to calculate the slop for one curve and to get all of the slopes together, I have to change some values again and again, but I prefer to do it manually and save them as a csv file. Can any one help me with this? I am totally new at matlab, any kind of help would be appreciated.
reading = csvread("test.csv");
x = reading(:,1);
y = reading (:,7);
x1q = find((x >= -0.6) & (x <=-0.45))% range -0.6 and -0.45
p=polyfit(x(x1q), y(x1q),1)
slope= p(1)
figure(2)
plot(x, y, '-b')
hold on
plot(x(x1q), y(x1q),'ok');

 채택된 답변

KSSV
KSSV 2021년 2월 5일
편집: KSSV 2021년 2월 5일

0 개 추천

You have to run a loop for each column for which you want to get slopes.
data = csvread("test.csv");
[m,n] = size(data) ;
slope = zeros(n-1,1) ;
x = data(:,1);
for i = 2:n
y = data (:,i); % pick each column
idx = find((x >= -0.6) & (x <=-0.45))% range -0.6 and -0.45
p=polyfit(x(idx), y(idx),1) ;
slope(i-1) = p(1)
end

댓글 수: 5

Minions
Minions 2021년 2월 5일
Thank you so much for the help. It really worked. But i have one question, after running the slope I got the results for 10 colums, where I have 9 colums in y axis, so I should get 9 results for slope right?
KSSV
KSSV 2021년 2월 5일
yes...you have nine data points (x,yi); i = 1:9..so you end up with 9 slopes.
Minions
Minions 2021년 2월 5일
hmm, but i ended up with 10 slopes, and one of them only have one and 0.
KSSV
KSSV 2021년 2월 5일
Yes you are right......edited the code...loop should be from 2 to n....
Minions
Minions 2021년 2월 5일
ok, thank you so much for the help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2021년 2월 5일

댓글:

2021년 2월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by