Avoiding zeros doesnt wrok while plotiing from a data file.
조회 수: 6 (최근 30일)
이전 댓글 표시
x=load(sprintf('test_line.dat'))
x1=x(:,1);
x1r=x(:,2);
% x1(x1r==0)=Nan %This shows erorr
x1r(x1r==0)=NaN;
x10=plot(x1,x1r,--b','LineWidth', 1.5);
%x20=plot(x1,x1r,.g','LineWidth', 1.5);
hold on
As long its line plot, it doenst work and connect next point with a line. If I use x1(x1r==0)=NAN, it still shows line connecting a gap. Using 'dot'as '.g' in the coommnet works fine, but if I want to plot many such plots overlapping, that will show only one color. Any sugegstions please?. Line plots would be betetr, if i can plot. Thanks...
Edit: Thes size of x1 or x1r =1450088 and that of nonzeros are x1(x1r~=0)=133604, if that is creating any problem (connecting gap while plotting nonzeros).
댓글 수: 2
Steven Lord
2025년 6월 25일
Are you certain that the elements of x1r are exactly zero or are they only close to zero?
x = [1e6 2.1 3e6 4.2]
While the second and fourth elements of x are small enough relative to the first and third elements that they display as 0.0000 they are not in fact equal to 0 as you can see if you extract them.
[x(2), x(4)]
If you wanted to make anything "small enough" equal to NaN, you can. In this case, I'm defining 10 as "small enough".
x(abs(x) < 10) = NaN
채택된 답변
Mathieu NOE
2025년 6월 24일
hello
one method is to use interpolation to fill the voids (NaN)
% Example data with NaN values
x = 1:10;
y = sin([1, 2, NaN, 4, 5, NaN, NaN, 8, 9, 10]/2);
% Interpolate to fill NaN values
y_filled = interp1(x(~isnan(y)), y(~isnan(y)), x, 'linear', 'extrap');
% plot the result
plot(x,y,'*',x,y_filled)
댓글 수: 14
Mathieu NOE
2025년 6월 26일
ok good
tx for accepting my answer
have a good day
fyi you are not obliged to repeat "hold on" inside the for loop , simply before the for loop is enough
this should give you the same result :
figure
hold on
linS = {'-','--',':','-.',':'};
l2={'A','B','C','D','E'};
lw={4.3,4.0,3.7,3.4,3.2};
for k = 1:size(y,2)
x0(k) = plot(x1(1:indm),y(1:indm,k),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% left block
plot(x1(indm+1:end),y(indm+1:end,:),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% right block
end
legend(x0([1, 2, 3, 4, 5]),{'$A$','$B$','$c$','$D$','$E$'},'Fontsize',46,'Location','Northwest', 'interpreter', 'latex');
legend('boxoff')
hold off
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







