why is there no lines in in my plot

4 views (last 30 days)
Alexander Juffras
Alexander Juffras on 11 Dec 2020
Answered: Samhitha on 6 Feb 2025
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

Answers (1)

Samhitha
Samhitha on 6 Feb 2025
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:

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!