plot with points in file

i have text file and have to create a function that will read a file which contain x and y cordinates for the data points and which will create a stem plot using n points random.

8 Comments

Have at it...many ways and examples to read simple text files, importdata one of simpler.
Only possibly odd thing here would be the random points, see
doc randperm
for easy way to handle that.
i have problem with stem function is reading file but how to pass n in the function which will plor random points?
Note that OP re-asked this question after deleting a dialog with over 15 comments between 3 people over the course of 3-5 hours.
The problem was never clearly defined and still isn't clearly defined.
@Roger Nadal you'll need to provide a sample of your text file and clearly explain how you'd like to use the data from the text file to produce a stem plot. It's unclear what the x and y values should be and where that data comes from. Untill all of that information is available I'm afraid the converstation will go in circles again.
The file has x and y value in it function should read the file plot n points
dpb
dpb on 10 Dec 2019
stem will have nothing to do with reading the file...of course, if the file has missing data, you'll have to deal with that. As Adam says, attach a file. HELP US HELP YOU, IT CERTAINLY WOULD HAVE BEEN RUDE TO HAVE DONE AS SAID. DON"T DO THAT!!!
For random set of n out of N, use randperm(N,n)
sample file
The sample file is helpful. How are you reading it in?
I notice the x values are all positive integers - perhaps they are index values. If I remember correctly from your deleted question, you did not want the stem plots to all fall on integer values along the x axis. So, I guess my question is, once you read the x and y values in, are they simply the inputs to stem(x,y)?
I am reading file using fopen but on x axis the points I want is total number of points in the file and after passing suppose n=20 it should plot 20 random points but the x axis should not changed

Sign in to comment.

Answers (2)

dpb
dpb on 11 Dec 2019
Edited: dpb on 11 Dec 2019
Well, still kinda' difficult to figure out exactly what is wanted, but as near as I can make out, something like;
t=readtable('file.txt'); % read the file
t=t(:,[2 4]); % get rid of the two x,y character columns
t.Properties.VariableNames={'x','y'}; % name two columns appropriately
% preliminaries out of way, the engine
nr=size(t,1); % rows in dataset
n=20; % arbitrary number points to plot initially and to choose randomly
ix=sort([1:n randperm(nr-n,n)+n]); % build the indexing vector of first n and n random thereafter
figure
stem(t.x(ix),t.y(ix)) % and stem() plot those...
QED

20 Comments

I am not observing any difficulty with the plot using that code.
dpb
dpb on 11 Dec 2019
The difficulty I see is an orphaned file handle...
the points on x axis with that code is different.stemp.png
dpb
dpb on 11 Dec 2019
What is n?
n is number of points at random
None of the y values in the file you supply are greater than 1, so the plot you show here with values exceeding 3 is the plot for some different program. You should close all your figures so that you do not get confused.
No the figure is correct y axis has value from 0-1 the 3 must be 0.3
function xyz(file,n)
fid = fopen(file,'r');
a = textscan(fid,'x %f y %f');
fclose(fid);
x = cell2mat(a(1));
y = cell2mat(a(2));
stem(x(1:n), y(1:n))
(This is your code just cleaned up slightly)
Run as:
xyz('file.txt', 100)
Produces this:
I do not see any missing values.
if i give n=10 so the values on x axis is change to [0-9]
So, set xlim() where want it.
There's still orphaned file handle in the function even after Walter's cleanup.
function xyz(file,n)
[x,y]=textread(file,'x %f y %f');
stem(x(1:n), y(1:n))
xlim([x(1), x(end)])
I have done that but then points is plot is only on (0-10)
dpb
dpb on 11 Dec 2019
Well, what would you expect?
Draw a picture by hand of what you think this should look like and attach...it's impossible to figure out what you expect from description so far.
See the earlier figure that plot 10 points with x axis is [0 to end of file points]
This is the spiral we got into last time.
@Roger Nadal, we really can't help you at all if you don't provide us with a picture of what you want. I don't understand your refusal to cooperate when so many volunteers are reaching out to you asking for the same thing.
So you want to use the y values from the text file but you want the x values to be randomized?
N points will plot any n number of points from file
dpb
dpb on 11 Dec 2019
Edited: dpb on 11 Dec 2019
That's what my Answer above does except I thought you also wanted the first n consecutively, not just n at random.
Change the above to
ix=randperm(N,n);
Would have thought that obvious.
Set the xlim range as desired.
I would add an xlim([0 max(x)]) as otherwise the full x axis will not be displayed if the randomly chosen x locations are all less than 90.
dpb
dpb on 11 Dec 2019
My understanding is he's actually wanting |xlim([0 x(end)]) regardless, so that's what I recommended above earlier.
But, expectations are so nebulous and getting information like pulling hens' teeth so I just said "salt to taste" this time...
Reminds of a DOE manager I worked for as consultant lo! those many years ago. I characterized working for him as being instructed "Bring me a rock!" When the rock was delivered, it was always "No, not that rock!" but could never say just which variety/size/shape/color of rock was desired until, finally, he either ran out of budget or (usually) would after third or fourth iteration suddenly decide the first or second was actually the one he was looking for after all... :)

Sign in to comment.

Assume data stored in this form
1 23
2 34
3 54
The code is
file = load('data.txt');
n=3;
x=file(randi(10,1,n),1); % 10:max 1:min n: points number
y=file(x,2);
stem(x,y)

Categories

Tags

Asked:

on 10 Dec 2019

Commented:

dpb
on 11 Dec 2019

Community Treasure Hunt

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

Start Hunting!