Plot points that meet a specific condition by using a function
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Simone Speltoni
am 19 Jun. 2021
Beantwortet: dpb
am 19 Jun. 2021
Hi all, I'm having some issue on this task: I would like to plot some data, from a .txt file, in a x-y plot, just when a "third" condition occurs. Data are inserted into columns into a table, so they are all together. At the end of the task data will be not equally spaced, but this part is well-explained here (https://it.mathworks.com/matlabcentral/answers/326049-how-to-plot-non-equally-spaced-data-in-an-equally-spaced-fashion). So data can be seen as variables x,y,z,a,b,c,d...etc, sorted in columns. The idea is to choose a condition (for example, when x=1) and plot the correspondant data from two other variables.
Here's an example of my code, that works on a txt file of 4 columns:
clear all
close all
D = readtable('Example.txt');
[numRows,numCols] = size(D);
num=table2array(D(:,1));
id=table2array(D(:,2));
Err=table2array(D(:,3));
slope=table2array(D(:,4));
%condition that has to occurr to find x and y: for example num==1
xnum=find(num==1);
L=length(xnum);
for j=1:L
idvector(j)=id(xnum(j)) %create 2 vectors containing points
Errvector(j)=Err(xnum(j)) %that match the condition
end
%not equally spaced points
xl=1:L;
plot(xl,Errvector,'linestyle','none','marker','.')
xlim([0,L+1]);
xticks(1:L)
set(gca, 'XTick',(1:L),'XTickLabel',idvector);
My code works fine, but I would like to make it smarter: is there a chance to create a sort of external function, where I choose the "condition" that I want to match, and select the two columns on which I want to apply this condition? I'm asking because it would become much more easier in case I have dozens of columns (=variables). Moreover, in my code I had to create idvector and Errvector, to make the plot; is it necessary? Would it be possible to incorporate these added arrays into the structure of the function?
P.s. I'm considering a function because I have some reminescence that it could make the code smoother, but I'm open to advice.
Thank you very much!
0 Kommentare
Akzeptierte Antwort
dpb
am 19 Jun. 2021
num=table2array(D(:,1));
id=table2array(D(:,2));
Err=table2array(D(:,3));
slope=table2array(D(:,4));xnum=find(num==1);
xnum=find(num==1);
L=length(xnum);
for j=1:L
idvector(j)=id(xnum(j)) %create 2 vectors containing points
Errvector(j)=Err(xnum(j)) %that match the condition
end
xl=1:L;
plot(xl,Errvector,'linestyle','none','marker','.')
...
is written more succinctly as
numVal=1; % The wanted value
ixNum=(D.num==numVal); % logical indexing/addressing array
plot(D.Err(ixNum),'linestyle','none','marker','.')
...
You created a table with named variables, use it instead of duplicating the same data.
And, yes, you can dispense entirely with all the temporaries, including the four before the two you mention explicitly.
To write a function, just pass in the name of the variable(s) you wish to plot and associated flag/value by which to select those of interest.
See the documentation for the table data class to see about all the modes of addressing; you can use character variables as dynamic addressing so can pass in the variable name of interest as well.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!