Effacer les filtres
Effacer les filtres

How to get multiple slopes from a file.

6 vues (au cours des 30 derniers jours)
Minions
Minions le 5 Fév 2021
Commenté : Minions le 5 Fév 2021
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');

Réponse acceptée

KSSV
KSSV le 5 Fév 2021
Modifié(e) : KSSV le 5 Fév 2021
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 commentaires
KSSV
KSSV le 5 Fév 2021
Yes you are right......edited the code...loop should be from 2 to n....
Minions
Minions le 5 Fév 2021
ok, thank you so much for the help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by