Plot points that meet a specific condition by using a function
古いコメントを表示
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 件)
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!