Avoiding zeros doesnt wrok while plotiing from a data file.

조회 수: 6 (최근 30일)
Anil
Anil 2025년 6월 24일
댓글: Mathieu NOE 2025년 6월 26일
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
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]
x = 1×4
1.0e+06 * 1.0000 0.0000 3.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)]
ans = 1×2
2.1000 4.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
x = 1×4
1000000 NaN 3000000 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Anil
Anil 2025년 6월 25일
Dear @Steven Lord, thank for that. I understand your point. If i plot with a mark '.',, it shows a gap between two points that makes sense that there are no points in between; generally connected by a line.

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

채택된 답변

Mathieu NOE
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
Anil
Anil 2025년 6월 26일
Dear @Mathieu NOE, I slighlty modified to show the overlapping lines as follow:
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 hold on;
hold on
plot(x1(indm+1:end),y(indm+1:end,:),'Color',colors(k,:),'linestyle',linS{k},'LineWidth', lw{k});% right block
hold on
end
legend(x0([1, 2, 3, 4, 5]),{'$A$','$B$','$c$','$D$','$E$'},'Fontsize',46,'Location','Northwest', 'interpreter', 'latex');
legend('boxoff')
Thank you very much.
Mathieu NOE
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개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by