plot with points in file
Show older comments
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
dpb
on 10 Dec 2019
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
on 10 Dec 2019
Adam Danz
on 10 Dec 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
on 10 Dec 2019
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)
Roger Nadal
on 10 Dec 2019
Adam Danz
on 10 Dec 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
on 10 Dec 2019
Answers (2)
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
Walter Roberson
on 11 Dec 2019
I am not observing any difficulty with the plot using that code.
dpb
on 11 Dec 2019
The difficulty I see is an orphaned file handle...
Roger Nadal
on 11 Dec 2019
dpb
on 11 Dec 2019
What is n?
Roger Nadal
on 11 Dec 2019
Walter Roberson
on 11 Dec 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
on 11 Dec 2019
Walter Roberson
on 11 Dec 2019
Edited: Walter Roberson
on 11 Dec 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
on 11 Dec 2019
dpb
on 11 Dec 2019
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
on 11 Dec 2019
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.
Roger Nadal
on 11 Dec 2019
Adam Danz
on 11 Dec 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
on 11 Dec 2019
Adam Danz
on 11 Dec 2019
So you want to use the y values from the text file but you want the x values to be randomized?
Roger Nadal
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.
Walter Roberson
on 11 Dec 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
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... :)
Bandar
on 11 Dec 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)
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
