why is there no lines in in my plot

조회 수: 12 (최근 30일)
Alexander Juffras
Alexander Juffras 2020년 12월 11일
답변: Samhitha 2025년 2월 6일
clear;
clc;
close all;
%input the filename you want to consider
%filename=input('what is the filename you want to consider \n','s');
folder ="C:\Users\Ajuff\CLionProjects\C++ML\C++MLP1\cmake-build-debug\tempvstime.txt";
%load file into variable data
data = load(folder);
for n=1:length(data)
fprintf('%f %f\n',data(n,1),data(n,2));
x=data(n,1);
y=data(n,2);
plot(x,y,'g')
end
why is there nothing in the plot the fprint f prints out values

답변 (1개)

Samhitha
Samhitha 2025년 2월 6일
Hi Alexander,
To visualize data points iteratively and connect them with lines, you can use MATLAB's “hold on” function. This allows you to add new data points to an existing plot without erasing the previous ones. By updating the plot in a loop, you can dynamically visualize how the data evolves over time. Additionally, by using the “plot” function with a line style input, you can connect the data points with lines.
You can modify your code as shown below:
% Load file into variable data
data = load(folder);
% Initialize arrays for x and y
x = [];
y = [];
% Open a new figure
figure;
hold on; % Retain plots so that new points can be added
% Iteratively add points and update the plot
for n = 1:length(data)
% Append new point to data
x = [x, data(n, 1)];
y = [y, data(n, 2)];
% Print the values
fprintf('%f %f\n', data(n, 1), data(n, 2));
% Plot the updated line with markers
plot(x, y, '-o');
end
hold off; % Release the plot hold
Hope this helps!
For more details, please refer to the following MathWorks Documentation:

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by