1

I'm trying to create a script that reads data from a text file, and plots the data onto a scatter plot.

For example, say the file name is prices.txt and contains:

Pens 2 4
Pencils 1.5 3
Rulers 3 3.5 
Sharpeners 1 3
Highlighters 3 4

Where columns 2 and 3 are prices of the items for two different stores.

What my script should do is read the prices, calculates (using another function) future prices of the stores and plots these prices onto a scatter plot where x is one store and y is another. This is a silly example I know but it fits the description.

Don't worry to much about the other function that does the calculation, just assume it does what its supposed to.

Basically, I've come up with the following:

pricesfile = fopen('Prices.txt');
prices = textscan(pricesfile, '%s %d d');
fclose(pricesfile);

count = 1;
while count <= length(prices{1})
    for item = constants{1}
        name = constants{1}{count};
        store_A = prices{2}{count};
        store_B = prices{3}{count};
        (...other function goes here...)
    end
end

After doing this I'm completely stuck. My thought process behind this was to go through each item name, and create a vector that's assigned to this name with its two corresponding prices as items in the vector eg:

pens = [2 4]
pencils = [1.5 3]

etc. Then, I would somehow plot those items in the vector on a scatter plot and use the name of the vector as a label.

I'm not too sure how to carry out the rest of my code or even if what I've written will get me to the solution.

Please help and thanks in advance.

1 Answer 1

2
pricesfile = fopen('Prices.txt');
data = textscan(pricesfile, '%s %d d');
fclose(pricesfile);

You were on the right track but after this (through a bit of hackery) you don't actually need a loop:

plot(repmat(data{2},1,2)', repmat(data{3},1,2)', '.')
legend(data{1})

What you DO NOT want to do is create variables named after strings. Rather store them in an array with an array of the names (which is basically what your textscan code gives you). Matlab is very good at handling matrices/arrays.

You could also split your price array up for example:

names = prices{1};
prices = [data{2:3}];

now you can perform calculations on prices quite easily like prices_cents = prices*100;

plot(prices_cents(:,[1,1]), prices_cents(:,[2,2]))
legend(names)

Note that the [1,1] etc above is just using indexing as a short hand to achieve what repmat does...

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, how would I use the other function I mentioned to first perform the necessary calculations on the data in the text file?
I'm having an issue trying to apply the same thing to a different sized cell array (10x3). Doing something like this: [code] plot(repmat(x{11:20})', repmat(x{21:30})', '.')[code] dosent yield the correct results.
Also, something like plot(x(11:20)) just yields an error "conversion from to double from cell is not possible"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.