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 Commenti

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.
Roger Nadal
Roger Nadal il 10 Dic 2019
i have problem with stem function is reading file but how to pass n in the function which will plor random points?
Adam Danz
Adam Danz il 10 Dic 2019
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.
Roger Nadal
Roger Nadal il 10 Dic 2019
The file has x and y value in it function should read the file plot n points
dpb
dpb il 10 Dic 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)
Roger Nadal
Roger Nadal il 10 Dic 2019
sample file
Adam Danz
Adam Danz il 10 Dic 2019
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)?
Roger Nadal
Roger Nadal il 10 Dic 2019
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

Accedi per commentare.

Risposte (2)

dpb
dpb il 11 Dic 2019
Modificato: dpb il 11 Dic 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 Commenti

Walter Roberson
Walter Roberson il 11 Dic 2019
I am not observing any difficulty with the plot using that code.
dpb
dpb il 11 Dic 2019
The difficulty I see is an orphaned file handle...
Roger Nadal
Roger Nadal il 11 Dic 2019
the points on x axis with that code is different.stemp.png
dpb
dpb il 11 Dic 2019
What is n?
Roger Nadal
Roger Nadal il 11 Dic 2019
n is number of points at random
Walter Roberson
Walter Roberson il 11 Dic 2019
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.
Roger Nadal
Roger Nadal il 11 Dic 2019
No the figure is correct y axis has value from 0-1 the 3 must be 0.3
Walter Roberson
Walter Roberson il 11 Dic 2019
Modificato: Walter Roberson il 11 Dic 2019
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.
Roger Nadal
Roger Nadal il 11 Dic 2019
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)])
Roger Nadal
Roger Nadal il 11 Dic 2019
I have done that but then points is plot is only on (0-10)
dpb
dpb il 11 Dic 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.
Roger Nadal
Roger Nadal il 11 Dic 2019
See the earlier figure that plot 10 points with x axis is [0 to end of file points]
Adam Danz
Adam Danz il 11 Dic 2019
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.
Roger Nadal
Roger Nadal il 11 Dic 2019
stem.png
Adam Danz
Adam Danz il 11 Dic 2019
So you want to use the y values from the text file but you want the x values to be randomized?
Roger Nadal
Roger Nadal il 11 Dic 2019
N points will plot any n number of points from file
dpb
dpb il 11 Dic 2019
Modificato: dpb il 11 Dic 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.
Walter Roberson
Walter Roberson il 11 Dic 2019
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 il 11 Dic 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... :)

Accedi per commentare.

Bandar
Bandar il 11 Dic 2019
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)

Categorie

Tag

Richiesto:

il 10 Dic 2019

Commentato:

dpb
il 11 Dic 2019

Community Treasure Hunt

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

Start Hunting!

Translated by