0

Context:

Currently, I have a map of my data where each key will have a value that is incremented, or if the key does not - it will create it - this works fine.

Problem:

However, when trying to plot via plot(keys(map),values(map)); this results in "Error using plot, not enough input arguments"

Code Example:

map = containers.Map({1},{0});

for i=1:10
    keyExists = isKey(map,i);

       
    if (keyExists == 0)
       map(i) = 0; 
    end
       
    map(i) = map(i) + 1;

 end

plot(keys(map),values(map));

Output Example:

>> keys(map)

ans =

  1×10 cell array

    {[1]}    {[2]}    {[3]}    {[4]}    {[5]}    {[6]}    {[7]}    {[8]}    {[9]}    {[10]}

I don't quite understand where the problem lies.

4
  • 2
    what does keys(map) output ? Please create a reproducible example, add the minimal code needed to reproduce your problem. Commented Nov 5, 2020 at 16:10
  • It produces a 1x20 cell array, the output appearing as {[1]} {[2]} {[...]} - the same with values(map). I will add example code. Commented Nov 5, 2020 at 16:20
  • Not sure, about the exact context but casting the outputs as matrices using the cell2mat() function allows plotting: plot(cell2mat(keys(map)),cell2mat(values(map))); Commented Nov 5, 2020 at 16:35
  • 1
    @Michaeltr7 That worked perfectly! Thank you. If you want to put it in a question format I'll accept it :) Commented Nov 5, 2020 at 16:38

1 Answer 1

1

Plotting Cell Arrays by Casting

Plotting outputs typically works best when the data is in the format of arrays. Here casting the arrays using the cell2mat() function will convert the cell (cell) arrays to mat (matrices) which can be plotted with ease.

plot(cell2mat(keys(map)),cell2mat(values(map)));

Ran using MATLAB R2019b

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

Comments

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.