How to plot a vector using a variable that contains the name of the vector.

A
ava.smith01 · Aug 27, 2020 · 2.3K views
Question
I have a vector called collection with some elements: collection = [1 2 3 4 5 6 7 8 9 0]; I also have a variable called x that stores the string 'collection' x = 'collection'; I would like to know how can I plot the values of the vector 'collection' by using the variable x that has the name of the vector stored inside it. I would like to do something like this: plot(x) and that the points from the vector collection get plotted.
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 25, 2026
You will need to use plot(eval(x)). If inside a function, use plot(evalin('base',x)).
Better way is to plan and re-organize your data structure. For example, use structure:
 
Data.collection=1:10;
x='collection';
plot(Data.(x))
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!